我有一个运行 ubuntu 14.04 的服务器,运行一个基于烧瓶的网站,其中包含一些我正在测试的自定义 API 功能。对于这个网站,我有一些使用 oauth2client 连接到 Youtube 的代码。这必须手动验证,并存储在 .json 文件中:
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
flow = flow_from_clientsecrets(self.YOUTUBE_CLIENT_SECRETS_FILE,
scope=self.YOUTUBE_READ_WRITE_SCOPE,
message=self.MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("{}/my-oauth2.json".format(os.path.dirname(os.path.realpath(__file__))))
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, self.args)
我已经在开始使用的 localhost 服务器上测试了此代码:
python run.py runserver
在这种情况下,它按预期工作。我只需要验证一次,API 调用就成功了。
>>> response = requests.post(url='http://localhost:5000/api', data=data)
>>> response.status_code
200
但是,当在服务器上将其作为 uwsgi/nginx 服务运行时,身份验证不会以某种方式被接受。os.path.isfile(<filename>)
使用返回检查凭据文件是否存在True
,但credentials
变量最终为None
,并且我的服务器希望我再次进行身份验证。这显然会导致我正在测试的 API 调用前 2 次超时,之后每次都失败。(这是预期的,因为它尝试进行身份验证)
>>> response = requests.post(url=my_api_url, data=data)
>>> response.status_code
504
>>> response = requests.post(url=my_api_url, data=data)
>>> response.status_code
504
>>> response = requests.post(url=my_api_url, data=data)
>>> response.status_code
502
我的问题是:为什么我存储的凭据适用于 localhost,但不适用于我的服务?如何修复它以便我可以使用 localhost 进行身份验证,并让服务器上的服务使用这些凭据?