Skip to main content
 首页 » 编程设计

python之使用 Python 访问 Google Calendar API

2025年05月04日14powertoolsteam

我目前正在开发一个简单的 native 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 的引用文档中找到的所有代码示例,但没有任何改变。

预先感谢您的帮助。

请您参考如下方法:

我用几乎相同的代码遇到了同样的问题。尝试将此行添加到您的 client_secrets.json 文件中: "access_type":"离线"

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

详情在这里:Google Calendar API v3 - How to obtain a refresh token (Python)