通过 google 应用引擎(只读文件系统)访问 google 搜索控制台 api

Access to google search console api through google app engine (read-only file system)

当我在 google 应用程序引擎应用程序中使用 oauth2client.file.storage.get() 时,它 returns IOError(errno.EROFS, 'Read-only file system', 文件名)。日志栈如下图:

File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\main.py", line 291, in main
run_wsgi_app(app)
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\webapp\util.py", line 101, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\webapp\util.py", line 119, in run_bare_wsgi_app
result = application(env, _start_response)
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\main.py", line 216, in get
response = service.searchanalytics().query(siteUrl=property_uri, body=request).execute()
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\googleapiclient\http.py", line 836, in execute
method=str(self.method), body=self.body, headers=self.headers)
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\googleapiclient\http.py", line 167, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\oauth2client\transport.py", line 186, in new_request
credentials._refresh(orig_request_method)
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\oauth2client\client.py", line 761, in _refresh
self._do_refresh_request(http)
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\oauth2client\client.py", line 802, in _do_refresh_request
self.store.locked_put(self)
File "c:\Users\john\AppData\Local\Google\Cloud SDK\gsc2gbq-test\oauth2client\file.py", line 85, in locked_put
f = open(self._filename, 'w')
File "C:\Users\john\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\python\runtime\stubs.py", line 278, in __init__
raise IOError(errno.EROFS, 'Read-only file system', filename) IOError: [Errno 30] Read-only file system: './webmaster_credentials.dat'

为了访问 Google Search Console API,我使用以下代码来验证和访问 google Search Console API。我认为这是问题的原因。

# Prepare the API service
    from oauth2client.file import Storage

    storage = Storage("webmaster_credentials.dat")
    credentials = storage.locked_get()

    http_auth = httplib2.Http()
    http_auth = credentials.authorize(http_auth)
    service = build('webmasters', 'v3', http=http_auth, cache_discovery=False)

我对此做了一些研究。有聪明人指出这是因为google应用引擎是只读系统

我的问题是:如何访问 Google 应用引擎中的 google 搜索控制台 API 以及如何解决该问题:IOError(errno.EROFS, 'Read-only file system', 文件名).

感谢您的回复或评论。

我自己的问题解决了。所以我 post 我的解决方案在这里,希望它能在您阅读本文时帮助您。

问题是必须刷新凭据文件的 short-live 令牌,因此必须将新令牌写入 google 应用程序引擎上的凭据文件。

我无法使用以下方法完成此操作:

from oauth2client.file import Storage

storage = Storage("webmaster_credentials.dat")
credentials = storage.locked_get()

相反,使用以下身份验证方法一劳永逸地解决了我的问题:P.

from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials(access_token=access_token, client_id=client_id, client_secret=client_secret,
                                    refresh_token=refresh_token, token_expiry=token_expiry, token_uri=token_uri, user_agent=user_agent, revoke_uri=revoke_uri)

您只需使用来自 google 应用引擎或其他 google api 的凭据定义变量。

祝你好运,调试愉快!