在Python中使用Google日历API v3
有人能给我一个清晰的解释,如何让谷歌日历API v3与Python客户端一起工作?具体来说,最初的OAuth阶段让我非常困惑。我所需要做的就是访问我自己的日历,阅读它,并对它进行更改。Google为配置我的应用程序提供以下代码:在Python中使用Google日历API v3,python,google-api,google-calendar-api,Python,Google Api,Google Calendar Api,有人能给我一个清晰的解释,如何让谷歌日历API v3与Python客户端一起工作?具体来说,最初的OAuth阶段让我非常困惑。我所需要做的就是访问我自己的日历,阅读它,并对它进行更改。Google为配置我的应用程序提供以下代码: import gflags import httplib2 from apiclient.discovery import build from oauth2client.file import Storage from oauth2client.client imp
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')
但是(a)这对我来说毫无意义;评论的解释很糟糕,(b)我不知道在变量中放什么。我已经在谷歌注册了我的程序,并注册了一个服务帐户密钥。但给我的只是一个要下载的加密密钥文件和一个客户端ID。我不知道什么是“developerKey”,或者什么是“client_secret”?那是钥匙吗?如果是,我如何得到它,因为它实际上包含在一个加密文件中?最后,考虑到我API使用的相对简单的目标(即,它不是多用户、多访问操作),有没有更简单的方法来实现这一点?谢谢 一个简单的方法是创建一个web应用程序,而不是一个服务帐户。这听起来可能很奇怪,因为你不需要任何类型的web应用程序,但我使用它的方式与你相同-对我自己的日历进行一些查询/添加事件/等等-所有这些都来自命令行,并且没有任何类型的web应用程序交互。有很多方法可以通过服务帐户实现这一点(如果你真的想走这条路,我会修改一下),但到目前为止,这对我来说是有效的
创建web应用程序后,您将获得上述所有信息(旁注:上面的示例代码基于web应用程序-要使用服务帐户,您的FLOW
需要调用FLOW\u from\u 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')
现在,您可以填写在API控制台中看到的值(client\u id
=整个client id
字符串,client\u secret
=客户机密,scope
是相同的,user\u agent
可以是您想要的任何内容)。对于服务
行,developerKey
是API控制台中简单API访问
部分下的API密钥(标签为API密钥
):
它通过查找该文件并验证内容来检查是否存在有效凭据(这些都是从您那里抽象出来的,以便更容易实现)。验证后,if
语句将计算False
,您将能够访问数据,而无需再次验证
希望这能让我们更清楚地了解这个过程——长话短说,制作一个web应用程序并使用其中的参数,进行一次身份验证,然后忘记它。我确信我忽略了很多方面,但希望它能对您的情况起作用。谷歌现在有一个很好的示例应用程序,可以让您轻松启动和运行。在他们的网站上提供“5分钟体验-快速入门” 页面
如果您在没有浏览器的远程服务器上工作,它将为您提供一个可直接访问的URL 在这个时候,它不给任何好处;你只需要得到一个生成的项目,它向你展示了如何OAuth,然后是一个“成功!现在在这里添加代码。”然后它会指出你应该自己去做的地方。我发现Google Calendar API v3文档非常神秘。
service = build(serviceName='calendar', version='v3', http=http,
developerKey='<your_API_key>')
events = service.events().list(calendarId='<your_email_here>').execute()
print events
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
credentials = run(FLOW, storage)