使用Python访问Google Calendar API

时间:2013-02-21 12:46:53

标签: python google-calendar-api

我目前正在开发一个简单的原生Python应用程序来与我的Google日历进行交互。为了做到这一点,我首次使用Google Calendar API,这要归功于Google Python库。

然而,尽管有文档,我仍然在我的日历中插入新事件而陷入僵局。以下是我连接和执行请求的代码的一部分:

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的参考文档中找到的所有代码示例,但没有任何变化。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我遇到了几乎相同代码的相同问题。尝试将此行添加到client_secrets.json文件中: “ACCESS_TYPE”: “离线”

这将确保您获得刷新令牌,而不是在一小时后过期的令牌。

详细信息:Google Calendar API v3 - How to obtain a refresh token (Python)