2

我正在尝试从 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 参数。

4

1 回答 1

3

app_identity更一般地说,服务帐户将无法正常工作,dev_appserver您必须回退常规的oauth2 网络服务器流程,以便在本地测试时获得与常规 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)

dev_appserver您可以使用SERVER_SOFTWARE 环境变量轻松检测您是在生产环境中运行还是在生产环境中运行。

于 2013-01-02T21:24:10.647 回答