使用Python访问Google日历API
我目前正在开发一个简单的本地Python应用程序,以便与我的Google日历交互。为了做到这一点,我第一次使用GoogleCalendarAPI,这要感谢GooglePython库 然而,尽管有文档记录,但在我的日历中插入一个新事件时,我陷入了僵局。以下是连接和执行请求的部分代码:使用Python访问Google日历API,python,google-calendar-api,Python,Google Calendar Api,我目前正在开发一个简单的本地Python应用程序,以便与我的Google日历交互。为了做到这一点,我第一次使用GoogleCalendarAPI,这要感谢GooglePython库 然而,尽管有文档记录,但在我的日历中插入一个新事件时,我陷入了僵局。以下是连接和执行请求的部分代码: import sys ... import httplib2 from apiclient.discovery import build from oauth2client.file import Storage f
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."
}
}
到目前为止,我已经尝试了很多东西,包括我在谷歌日历API的参考文档中找到的所有代码示例,但没有任何改变
提前感谢您的帮助。我用几乎相同的代码遇到了相同的问题。尝试将此行添加到您的client_secrets.json文件: “访问类型”:“脱机” 这将确保返回刷新令牌,而不是一小时后过期的令牌
此处的详细信息:您是否已在注册API帐户?当然可以。我忘了说我已经成功地尝试了Google文档中的一些示例代码。您还应该删除任何现有的credentials.dat文件,并强制用户使用新的client_secrets.json参数重新授权访问,以使其生效。从今天起(9/24/14)我无法使用flow_from_clientsecrets使刷新令牌正常工作,但它可以与OAuth2WebServerFlow一起工作,仅供参考