HttpAccessTokenRefreshError at ...... unauthorized_client:客户端未经授权使用此方法检索访问令牌

时间:2017-07-09 01:24:36

标签: python django google-oauth google-calendar-api

我为google calendar api项目设置了一个服务帐户。我的想法是,我的网站可以连接到我的谷歌日历和显示事件等。由谷歌提出

Service account clients are created when domain-wide delegation is 
enabled on a service account.

确实发生了这种情况,因此我有一个连接OAuth 2.0 client ID。到现在为止还挺好。我生成了一个用于连接的json文件,并尝试使用此代码进行身份验证

from cal.models import Cal as Caldb
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client import tools

from calendar import monthrange
from calendar_gui import MyCalendar
from datetime import datetime 
from datetime import date
from django.utils.safestring import mark_safe

from oauth2client.service_account import ServiceAccountCredentials
import os
from django.conf import settings

def authenticate():
    scope= ['https://www.googleapis.com/auth/calendar']
    credentials = ServiceAccountCredentials.from_json_keyfile_name(os.path.join(settings.PROJECT_ROOT, '../', 'myjson.json'), scopes=scope)
    delegated_credentials = credentials.create_delegated('email@email.com')

    http = httplib2.Http()
    http = delegated_credentials.authorize(http)
    service = build(serviceName='calendar', version='v3', http=http, credentials=credentials)

    return service

但是唉

service = build(serviceName='calendar', version='v3', http=http, credentials=credentials)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/discovery.py", line 214, in build
cache)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/discovery.py", line 261, in _retrieve_discovery_doc
resp, content = http.request(actual_url)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/transport.py", line 153, in new_request
credentials._refresh(orig_request_method)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 765, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 834, in _do_refresh_request
  raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
HttpAccessTokenRefreshError: unauthorized_client: Client is unauthorized to retrieve access tokens using this method.    

我启用了Google日历API。事实上,我觉得我已经遵循了所有步骤。事实上,我已经完成了这一次,取得了圆满成功,但我现在感到困惑。我读过,也许我需要等待24-48小时才能生效。可能就这么简单吗?

目前,我只是在本地进行测试。什么都没有推到服务器上。

1 个答案:

答案 0 :(得分:0)

检查此链接: Google API Python unauthorized_client: Unauthorized client or scope in request

您需要使用"开发者控制台中的客户端ID"作为"管理API客户端访问"中的客户名称在设置API范围时。