2013-02-21 198 views
1

我目前正在開發一個簡單的本機Python應用程序來與我的Google日曆交互。爲了做到這一點,我首次使用Google Calendar API,這要歸功於Google Python庫。使用Python的Google日曆API訪問

但是,儘管有文檔,但我在日曆中插入新事件時陷入了僵局。這裏是連接並執行我的要求我的代碼的一部分:

import sys 
... 
import httplib2 
from apiclient.discovery import build 
from oauth2client.file import Storage 
from oauth2client.client import AccessTokenRefreshError 
from oauth2client.client import flow_from_clientsecrets 
from oauth2client.tools import run 

... 

flow = flow_from_clientsecrets('client_secrets.json', 
    scope='https://www.googleapis.com/auth/calendar', 
    redirect_uri='http://localhost') 

storage = Storage('credentials.dat') 
credentials = storage.get() 
if credentials is None or credentials.invalid: 
    credentials = run(flow, storage) 

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

try: 
    event = { 
    "start": "2013-02-20T09:00:00.000+01:00", 
    "end": "2013-02-20T11:00:00.000+01:00", 
    "summary": "New event", 
    "location": "Paris, FRANCE" 
    } 
    service.events().insert(calendarId='primary', body=event).execute() 
    print "END" 
except AccessTokenRefreshError: 
    print ('Credentials have been revoked') 

一旦執行,這就是我的了:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "usageLimits", 
    "reason": "dailyLimitExceededUnreg", 
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", 
    "extendedHelp": "https://code.google.com/apis/console" 
    } 
    ], 
    "code": 403, 
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." 
} 
} 

我已經嘗試了很多事情,到目前爲止,包括所有的我通過Google Calendar API的參考文檔找到的代碼示例,但沒有任何更改。

在此先感謝您的幫助。

+0

你有沒有在https://code.google.com/apis/console的API註冊帳戶? – 2013-02-21 13:00:16

+0

當然可以。我忘了說我已經嘗試了Google文檔中的一些示例代碼,並取得了成功。 – aseure 2013-02-21 13:24:58

回答

0

我遇到了幾乎相同的代碼相同的問題。嘗試將此行添加到client_secrets.json文件中: 「access_type」:「offline」

這將確保您獲取刷新令牌並且不會在一小時後過期的令牌。

詳情點擊這裏:Google Calendar API v3 - How to obtain a refresh token (Python)

+0

您還應該刪除任何現有的credentials.dat文件,並強制用戶使用新的client_secrets.json參數重新授權訪問權限,以使其起作用。 – 2014-01-14 04:26:37

+0

截至今天(2014年9月24日)我無法使用flow_from_clientsecrets獲取刷新令牌,但它對OAuth2WebServerFlow正常工作,僅供參考 – grokpot 2014-09-24 23:32:14