使用Google Calendar API v 3和Python

use*_*661 12 python google-calendar-api google-api

有人可以给我一个明确的解释,说明如何让Google Calendar API v3与Python客户端一起使用吗?具体来说,最初的OAuth阶段让我很困惑.我需要做的就是访问我自己的日历,阅读它并对其进行更改.Google提供此代码来配置我的应用:

import gflags
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run

FLAGS = gflags.FLAGS

# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console
FLOW = OAuth2WebServerFlow(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    scope='https://www.googleapis.com/auth/calendar',
    user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')

# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False

# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW, storage)

# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)

# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
service = build(serviceName='calendar', version='v3', http=http,
       developerKey='YOUR_DEVELOPER_KEY')
Run Code Online (Sandbox Code Playgroud)

但是(a)这对我来说完全没有意义; 评论解释很糟糕,(b)我不知道在变量中放什么.我已经在Google上注册了我的程序并注册了服务帐户密钥.但是所有这些都是我要下载的加密密钥文件和客户端ID.我不知道"developerKey"是什么,或者什么是"client_secret"?这是关键吗?如果是,我该如何获取它,因为它实际上包含在加密文件中?最后,考虑到我使用API​​的相对简单的目标(即,它不是多用户,多访问操作),有没有更简单的方法来做到这一点?谢谢.

Roc*_*key 13

一个简单的(读取:我已经完成它的方式)方法是创建Web应用程序而不是服务帐户.这可能听起来很奇怪,因为您不需要任何类型的Web应用程序,但我以与您相同的方式使用它 - 对我自己的日历/添加事件/等进行一些查询. - 所有这些都来自命令行,没有任何类型的网络应用程序交互.有一些方法可以通过一个服务帐户来实现(如果你确实想要继续沿着这条路线,我会修补一下),但到目前为止,这对我有用.

创建Web应用程序后,您将获得上面显示的所有信息(旁注:上面的示例代码基于Web应用程序 - 使用您FLOW需要调用的服务帐户flow_from_clientsecrets并进行进一步调整 - 请参阅在这里).因此,您可以填写此部分:

FLOW = OAuth2WebServerFlow(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    scope='https://www.googleapis.com/auth/calendar',
    user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')
Run Code Online (Sandbox Code Playgroud)

您现在可以使用您在API控制台中看到的值填写(client_id=整个Client ID字符串,client_secret=客户端密码,scope是相同的,user_agent可以是您想要的任何内容).至于该service行,developerKey您可以Simple API Access在API控制台的部分下找到API密钥(标签是API key):

service = build(serviceName='calendar', version='v3', http=http, 
    developerKey='<your_API_key>')
Run Code Online (Sandbox Code Playgroud)

然后,您可以添加如下所示的简单检查,看看它是否有效:

events = service.events().list(calendarId='<your_email_here>').execute()
print events
Run Code Online (Sandbox Code Playgroud)

现在,当您运行此操作时,将弹出一个浏览器窗口,允许您完成身份验证流程.这意味着所有身份验证都将由Google处理,身份验证响应信息将存储在其中calendar.dat.该文件(将与脚本存储在同一目录中)将包含服务现在将使用的身份验证信息.这就是这里的情况:

storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW, storage)
Run Code Online (Sandbox Code Playgroud)

它通过查找该文件并验证内容来检查是否存在有效凭据(这些都是从您那里抽象出来的,以便更容易实现).进行身份验证后,该if语句将进行评估False,您无需再次进行身份验证即可访问数据.

希望能够更好地了解这个过程 - 长话短说,创建一个Web应用程序并使用其中的参数,进行一次身份验证然后忘记它.我敢肯定我有几点可以忽略,但希望它能适合你的情况.