【问题标题】:Trouble with oauth2client.client.Storageoauth2client.client.Storage 出现问题
【发布时间】:2015-09-15 18:10:42
【问题描述】:

我正在尝试将 OAuth2 与我的 App Engine 应用程序一起使用,但我不断收到以下错误:

Encountered unexpected error from ProtoRPC method implementation: IOError ([Errno 30] Read-only file system: 'credentials.dat')
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
    response = method(instance, request)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote
    return remote_method(service_instance, request)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method
    response = method(service_instance, request)
  File "/base/data/home/apps/s~art-everywhere/7.385356767964097452/main.py", line 438, in upload_putphoto
    gd_client = PicasaWA.login()
  File "/base/data/home/apps/s~art-everywhere/7.385356767964097452/main.py", line 1653, in login
    storage.put(credentials)
  File "/base/data/home/apps/s~art-everywhere/7.385356767964097452/oauth2client/client.py", line 325, in put
    self.locked_put(credentials)
  File "/base/data/home/apps/s~art-everywhere/7.385356767964097452/oauth2client/file.py", line 113, in locked_put
    f = open(self._filename, 'wb')
IOError: [Errno 30] Read-only file system: 'credentials.dat'
看起来库中使用的写入存在问题。

这是我使用的代码的 sn-p:

def login(cls):
    scope = 'https://picasaweb.google.com/data/'
    user_agent = 'picasawebuploader'
    # credential_store = os.path.join(os.path.split(__file__)[0], "credentials.dat")

    storage = Storage("credentials.dat")
    # storage = Storage(credential_store)
    credentials = storage.get()

    # user = users.get_current_user()
    # storage = StorageByKeyName(CredentialsModel, user.user_id(), 'credentials')
    # credentials = storage.get()

    if credentials is None or credentials.invalid:
        flow = flow_from_clientsecrets("client_secrets.json", scope=scope, redirect_uri='urn:ietf:wg:oauth:2.0:oob')
        uri = flow.step1_get_authorize_url()
        logging.info("uri: %s", uri)
        webbrowser.open(uri)
        code = "Here I posted the code retrived by the autentication"
        credentials = flow.step2_exchange(code)
        storage.put(credentials)

    if (credentials.token_expiry - datetime.utcnow()) < timedelta(minutes=5):
        http = httplib2.Http()
        http = credentials.authorize(http)
        credentials.refresh(http)

    gd_client = gdata.photos.service.PhotosService(source=user_agent,
                                                   email=USERNAME,
                                                   additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token})

    return gd_client

我做错了什么? 感谢大家的帮助!

【问题讨论】:

    标签: python google-app-engine oauth-2.0 google-oauth gdata-api


    【解决方案1】:

    您不能像在常规文件系统 it's read-only 上那样在 AppEngine 中创建文件。您需要使用特殊的AppEngine credentials and storage objects

    【讨论】:

    • 是的,我知道这个应用引擎限制。但是,正如您在代码 sn-p 中看到的,我使用了 App Engine Oauth2 库提供的特殊存储对象。看着教程,在我看来我遵循了正确的步骤......你能告诉我我做错了什么以及如何更正代码以使其工作吗?如果您能帮我解决这个烦人的问题,我将非常感谢您。
    【解决方案2】:

    在我的本地机器上运行 devserver 时遇到了同样的问题。该解决方案并不漂亮,但this change 为我解决了它。我不确定您是否可以在 appengine 中运行 storage.put(credentials)。这可能是很多问题的来源。将 oauth 流程作为单独脚本的一部分执行,然后在修改 devserver 后从文件中加载凭据实际上对我有用。

    我非常怀疑这是否真的会在生产中起作用,所以你可能想看看Delegating domain-wide authority to the service account

    【讨论】:

    • 我还获得了运行单独脚本的凭据,但访问令牌可能会过期,并且如您所说,它不适合生产。谢谢你的链接!我终于让它工作了。应该更新 Sheet Api 示例,因为 oauth 流程会重复多次,并且它会尝试将凭据写入文件(AppEngine 不支持)。
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多