使用Python的Google日历API访问

问题描述:

我目前正在开发一个简单的本机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

我遇到了几乎相同的代码相同的问题。尝试将此行添加到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