谷歌服务帐户Http错误500 Python

2024-04-23 11:26:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在想办法解决这个问题,所以我希望有人能帮忙。在

使用服务帐户访问GoogleAPI端点似乎是相对简单的代码,但是我得到了Http错误500

它可以很好地与Tasks API一起工作,同样的身份验证。在

我的源代码:

import httplib2
import pprint
import sys

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

f = file('key.p12', 'rb')
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
'403695561042-6ntna04usrl5sscg3ovij6t4d8vfvsqp@developer.gserviceaccount.com',
key,
scope='https://www.googleapis.com/auth/apps.order.readonly')

http = httplib2.Http()
http = credentials.authorize(http)
service = build("reseller", "v1", http=http)
lists = service.subscriptions().list().execute(http=http) 
print lists

回应:

^{pr2}$

回溯:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/admin/Sites/newmind/reseller-api/app.py", line 79, in index
    lists = service.subscriptions().list().execute(http=http)
  File "build/bdist.macosx-10.8-intel/egg/oauth2client/util.py", line 120, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "build/bdist.macosx-10.8-intel/egg/apiclient/http.py", line 678, in execute
    raise HttpError(resp, content, uri=self.uri)

直接从Google示例中复制代码,只更改了服务名称和身份验证。 http://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py tasks API使用相同的凭证返回http200。在

当我使用相当标准的httplib2请求时,我得到的响应是:

WARNING:oauth2client.util:new_request() takes at most 1 positional argument (2 given)
    {'status': '500', 'content-length': '52', 'x-xss-protection': '1; mode=block', 'x-content-type-options': 'nosniff', 'expires': 'Sat, 20 Oct 2012 06:25:19 GMT', 'server': 'GSE', '-content-encoding': 'gzip', 'cache-control': 'private, max-age=0', 'date': 'Sat, 20 Oct 2012 06:25:19 GMT', 'x-frame-options': 'SAMEORIGIN', 'content-type': 'application/json; charset=UTF-8'}
    {
     "error": {
      "code": 500,
      "message": null
     }
    }

如果有人能帮忙,我将非常感激。在

谢谢


Tags: inpyimportselfapphttpflaskrequest
2条回答

不使用该流程,可以执行以下操作:

您得到的错误是由于以下事实造成的:a)对API进行了正确的身份验证,但是b)您正在使用的用户无权访问您尝试访问的帐户。在

我也遇到了同样的问题,通过将OAuth2服务帐户的电子邮件地址添加到分析帐户的用户中,解决了这个问题。在

对于您要访问的每个帐户,您必须单独添加。在

有点遥不可及,但您是否在项目控制台中打开了经销商API?我记得在尝试访问我最初使用的服务之外的服务时也遇到过类似的问题。在

编辑:根据下面的注释,另一种解决方案是使用流并将access_type设置为offline,以允许刷新令牌,而不需要用户不断地重新验证。在

相关问题 更多 >