【问题标题】:Why am I getting invalid_grant for Google API calls from App Engine?为什么我会收到来自 App Engine 的 Google API 调用的 invalid_grant?
【发布时间】:2013-01-03 05:24:10
【问题描述】:

我正在尝试从 App Engine 访问 Google Prediction API 并按照此处的说明进行操作—— https://developers.google.com/appengine/articles/prediction_service_accounts

这在 App Engine 上部署时效果很好。但是,相同的代码在本地开发服务器上失败并出现以下错误。

credentials = AppAssertionCredentials(
              scope='https://www.googleapis.com/auth/prediction')
http = credentials.authorize(httplib2.Http(memcache))
service = build("prediction", "v1.5", http=http, developerKey=api_key)

ERROR    2012-12-28 03:48:53,084 client.py:461] Failed to retrieve access token: {
  "error" : "invalid_grant"
}
ERROR    2012-12-28 03:48:53,115 cgi.py:121] Traceback (most recent call last):
  File "/Users/gkedia/git/thirdgaze/main.py", line 83, in <module>
    service = build('prediction', 'v1.5', http=http, developerKey=api_key)
  File "/Users/gkedia/git/thirdgaze/apiclient/discovery.py", line 175, in build
    resp, content = http.request(requested_url)
  File "/Users/gkedia/git/thirdgaze/oauth2client/client.py", line 503, in new_request
    self._refresh(request_orig)
  File "/Users/gkedia/git/thirdgaze/oauth2client/client.py", line 412, in _refresh
    self._do_refresh_request(http_request)
  File "/Users/gkedia/git/thirdgaze/oauth2client/client.py", line 472, in _do_refresh_request
    raise AccessTokenRefreshError(error_msg)
AccessTokenRefreshError: invalid_grant

我注意到的一件事是对于完全相同的参数,key_name, signature = app_identity.sign_blob(base_str) 在生产和本地机器上返回不同的签名。

我的电脑时间已正确同步,而且还没有涉及到offline_access参数。

【问题讨论】:

  • 是在使用开发服务器的本地计算机上失败还是在 AppEngine 上部署时失败?
  • 仅在本地计算机上失败。 (经过澄清的编辑问题)

标签: python google-app-engine


【解决方案1】:

app_identity 和更一般的服务帐户无法在 dev_appserver 上运行,您必须回退常规的 oauth2 webserver flow 才能在本地测试时获得与常规 Google 帐户关联的访问令牌。

类似:

flow = OAuth2WebServerFlow(client_id='your_client_id',
                           client_secret='your_client_secret',
                           scope='https://www.googleapis.com/auth/prediction',
                           redirect_uri='http://localhost:8080/oauth2callback')
self.redirect(flow.step1_get_authorize_url())

然后在/oauth2callback处理程序中:

credentials = flow.step2_exchange(self.request.get('code'))
http = credentials.authorize(httplib2.Http(memcache))
service = build("prediction", "v1.5", http=http, developerKey=api_key)

您可以使用SERVER_SOFTWARE environment variable 轻松检测您是在dev_appserver 上运行还是在生产中。

【讨论】:

  • 您知道dev_appserver.py 是否仍然无法与 Google OAuth 2.0 服务帐户一起使用,还是已经更新?
  • 什么是memcache和developerKey?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-03-05
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多