gpt4 book ai didi

python - 授权 python 脚本在没有 OAuth2 用户流程的情况下访问 GData API

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:58 25 4
gpt4 key购买 nike

我正在编写一个小型 python 脚本,它将检索我的 Google 联系人列表(使用 Google Contacts API ),并随机建议我联系一个人(自动与 friend 保持联系的好方法!)

这只是一个独立的脚本,我计划将其安排在 cron 作业上。问题是 Google 似乎需要 OAuth2 风格的身份验证,其中用户(我)必须批准访问,然后应用程序会收到一个授权 token ,我可以用它来查询用户(我)的联系人。

由于我只访问自己的数据,有没有办法对自己进行“预授权”?理想情况下,我希望能够检索一些授权 token ,然后运行脚本并将该 token 作为环境变量传递

AUTH_TOKEN=12345 python my_script.py

这样就不需要用户输入/交互来授权一次。

最佳答案

您描述的实现调用完整的“三足”OAuth 握手,这需要明确的用户同意。如果您不需要用户同意,您可以通过 Google service account 使用“两条腿”OAuth ,它与应用程序绑定(bind),而不是用户。一旦你granted permission要访问您的联系人,您可以使用 oauth2client 到您的服务帐户ServiceAccountCredentials class无需用户同意即可直接访问GData。

这是来自 Google service account documentation 的两条腿身份验证示例:

import json

from httplib2 import Http

from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build

scopes = ['https://www.googleapis.com/auth/sqlservice.admin']

credentials = ServiceAccountCredentials.from_json_keyfile_name(
'service-account.json', scopes)

sqladmin = build('sqladmin', 'v1beta3', credentials=credentials)
response = sqladmin.instances().list(project='examinable-example-123').execute()

print response

关于python - 授权 python 脚本在没有 OAuth2 用户流程的情况下访问 GData API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38669200/

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