gpt4 book ai didi

python - 如何在 Linux 上首次使用 Google Calendar API?

转载 作者:行者123 更新时间:2023-12-01 01:14:46 24 4
gpt4 key购买 nike

您可以使用此 link 中的 google-calerdar-api .

我使用此代码(python3.X)来获取Windows 10上的日历数据。

Python 代码:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/calendar'

def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('./credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('calendar', 'v3', http=creds.authorize(Http()))
print('Getting the upcoming 10 events')
events_result = service.events().list(calendarId='primary', alwaysIncludeEmail=True,
timeMin='2017-01-01T00:00:00+09:00',timeMax='2019-01-05T23:59:59+09:00',
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])

if __name__ == '__main__':
main()

当我第一次使用这个api的示例代码时,此信息出现在pycharm控制台中。

Your browser has been opened to visit:

https://accounts.google.com/o/oauth2/auth?client_id=1132573726-nu1pktj3jt03qjigful1cq32p97eu.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&access_type=offline&response_type=code

If your browser is on a different machine then exit and re-run this
application with the command-line parameter

--noauth_local_webserver

然后我的浏览器弹出这样的页面。 enter image description here

然后我点击了我的帐户。

在此页面上,我单击了允许

enter image description here

然后浏览器会显示该消息。

enter image description here

然后pycharm的控制台出现这样:

Authentication successful.
Getting the upcoming 10 events
2018-01-15T08:30:00+09:00 20180115
2019-01-01T10:00:00+09:00 hello
2019-01-02T11:00:00+09:00 20190102_test1
2019-01-03T11:30:00+09:00 20190103_test1
2019-01-04T11:00:00+09:00 20190104_test1

所以代码运行成功。我在代码文件夹中看到文件 token.json

但是我的问题是,我无法在Linux(centos)上使用浏览器,那么如何让我的帐户第一次像Windows一样操作呢?

最佳答案

Ouaht2 和用户同意

如果您想访问用户的私有(private)数据,那么您始终必须让他们至少对您的应用程序进行一次身份验证。唯一的方法是使用网络浏览器窗口。

发生的第一件事是用户必须通过浏览器登录他们的谷歌帐户,登录后他们将看到同意屏幕,要求他们允许您的应用程序访问他们的数据。

通过这种方式,用户同意您的应用程序访问他们的数据。

针对 Linux 用户的开发。

我很好奇为什么您认为 Linux 用户将运行您的应用程序。我还没有看到很多人设计在 Linux 上运行的 native 应用程序。 WTG 你。

运行 Linux 的用户必须打开 Web 浏览器,这可以通过 Windows 桌面完成。如果他们正在运行命令行,那么他们将必须复制链接并在浏览器上打开它。我不认为命令行 Web 浏览器在这里可以工作,它必须通过 Linux 桌面来完成。

服务帐户

现在,当您似乎担心 Linux 用户时,我想知道您是否实际上并不真正担心应用程序的用户。

您是否正在尝试在 Linux 服务器上以 cron 作业的方式运行此应用程序?还假设您仅尝试访问您个人控制的单个帐户。然后您可以使用所谓的服务帐户。服务帐户用于服务器到服务器的通信,在登录时实际上没有用户在场来批准同意。为了使服务帐户正常工作,必须预先批准它们。在 Google 开发者控制台中创建服务帐户后,您只需获取服务帐户电子邮件地址并通过 Google 日历网站共享您希望其访问的日历即可。与服务帐户共享日历后,它将可以访问该日历,并且您不必担心网络浏览器身份验证。

关于python - 如何在 Linux 上首次使用 Google Calendar API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54456292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com