2

我已經爲我的Google日曆API項目設置了服務帳戶。我們的想法是,我的網站能夠連接到我的谷歌日曆和展示活動等。因爲它是由谷歌HttpAccessTokenRefreshError at ... unauthorized_client:客戶端未經授權使用此方法檢索訪問令牌

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 protected]') 

    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小時才能生效。它可以這麼簡單嗎?

現在,我只是在本地進行測試。沒有推送到服務器。

回答

相關問題