python - 使用 Python 访问 Google Calendar API

标签 python google-calendar-api

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

关于python - 使用 Python 访问 Google Calendar API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15002633/

相关文章:

python - 球形地球上二维矢量场的发散(Python/ basemap )

python - 传出负载均衡器

python - 使用 Python 进行阶乘计算

java - 如何从我的 Android 应用程序中删除或编辑 G-calendar 中的事件?

python - Google Calendar API 仅获取日历的第一个事件

python - 有什么方法可以在不使用 shell=True 的情况下使用子进程模块在 Python 中执行管道命令?

python - 根据外部条件更改数据框模型

javascript - 重复规则错误 400 错误请求 - Google Api 日历

android - 如何使用android使用日历API在谷歌日历上添加事件?

ruby - 使用 Ruby 从公共(public) Google 日历获取数据