【问题标题】:HttpAccessTokenRefreshError at ... unauthorized_client: Client is unauthorized to retrieve access tokens using this methodHttpAccessTokenRefreshError at ...未经授权的客户端:客户端未授权使用此方法检索访问令牌
【发布时间】:2017-07-09 01:24:35
【问题描述】:

我已经为我的 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.    

我已启用谷歌日历 API。事实上,我觉得我已经完成了所有步骤。事实上,我以前做过一次并取得了完全的成功,但我现在很困惑。我读到也许我需要等待 24-48 小时才能让这一切生效。就这么简单吗?

目前,我只是在本地进行测试。尚未向服务器推送任何内容。

【问题讨论】:

  • 你能解决这个问题吗?

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


【解决方案1】:

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

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

【讨论】:

  • 我看到了,但我只是想连接为我自动生成的json 文件以及客户端密钥。
猜你喜欢
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 2019-01-27
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 2022-09-19
相关资源
最近更新 更多