我想从我的日历中获取事件列表到我的 python 程序,但我无法通过我的 Google 理解 oauth 的大量文档。
这是我正在尝试的代码。它的半点点滴滴。谁能帮我解决这个问题
import gflags
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from oauth2client.client import flow_from_clientsecrets
flow = flow_from_clientsecrets('client_secrets.json',
scope='https://www.googleapis.com/auth/calendar',
redirect_uri='http://example.com/auth_return')
http = httplib2.Http()
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http,
developerKey='AI6456456456456456456456456456yKhI')
events = service.events().list(calendarId='645645654646a2cb4fl164564564@group.calendar.google.com').execute()
print events
问题是我如何从我的流对象中获取凭据
我在 API 控制台中创建了一个项目并向其中添加了日历 API。将它们放在 clients.json 中。
我不想要浏览器重定向及其我想要访问的我的日历。我不希望用户访问他们的日历,而是访问我的日历,以便我可以在网站上发布我的事件。
我不知道为什么我必须使用 Oauth 来访问我的日历
请您参考如下方法:
如果我没记错的话,您可以使用 v2 api(已弃用),它允许在没有 oAuth 的情况下登录。对于 v3,这是我所拥有的身份验证流程并且它有效(注意,我不使用 flow_from_clientsecrets):
import gflags
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
flags = gflags.FLAGS
flow = OAuth2WebServerFlow(
client_id = 'some_id.apps.googleusercontent.com',
client_secret = 'some_secret-32ijfsnfkj2jf',
scope='https://www.googleapis.com/auth/calendar',
user_agent='Python/2.7')
# to get a link for authentication in a terminal,
# which needs to be opened in a browser anyway
flags.auth_local_webserver = False
# store auth token in a file 'calendar.dat' if it doesn't exist,
# otherwise just use it for authentication
base = os.path.dirname(__file__)
storage = Storage(os.path.join(base, 'calendar.dat'))
credentials = storage.get()
if credentials is None or credentials.invalid == True:
credentials = run(FLOW, storage)
http = httplib2.Http()
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http,
developerKey='AI6456456456456456456456456456yKhI')
然后使用service进行通信。请注意,此代码可能缺少一些导入。如果我没记错的话,如果您访问的是主日历,则无需提供日历 ID。
希望对你有帮助。
