gpt4 book ai didi

python - 如何在没有人工交互的情况下使用 Google Drive SDK 2 和 Python 获取 OAuth 凭证

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:51 24 4
gpt4 key购买 nike

我有一个 Python 应用程序,它从第三方来源获取数据,选择一个子集,并将其写入 CSV 文件,然后上传到 Google 云端硬盘并转换为电子表格。我想将其作为 cron 作业运行,但目前 SDK 需要人工与浏览器交互以获取 OAuth 凭据。

我搜索了 Drive SDK 文档,但没有找到任何帮助。我还尝试使用 mechanize 库自动执行 OAuth 流程,但没有成功。我确定我错过了什么。我无法相信 Drive API 需要人工交互。有什么建议吗?

我咨询过https://developers.google.com/accounts/docs/OAuth2WebServer#overview这就是我尝试过的。

flow = OAuth2WebServerFlow(settings.CLIENT_ID, settings.CLIENT_SECRET
setings.OAUTH_SCOPE,settings.REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()

创建请求 url:

https://accounts.google.com/o/oauth2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&
redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&
response_type=code&
client_id=999637210521.apps.googleusercontent.com&
access_type=offline

然后使用我发出的请求库:
r = req.get(authorize_url)

但响应正文是一个没有代码的 HTML 文档。

最佳答案

我之前也有同样的问题。

解决方案:保存凭据以备将来使用。请引用以下网址:

https://developers.google.com/accounts/docs/OAuth2#installed

https://developers.google.com/accounts/docs/OAuth2InstalledApp

from oauth2client.file import Storage
...
storage = Storage('a_credentials_file')
storage.put(credentials)
...
credentials = storage.get()

我将我的脚本推送到 github,在该脚本中我重复使用凭据来创建新 token 。

https://github.com/sukujgrg/google_drive

关于python - 如何在没有人工交互的情况下使用 Google Drive SDK 2 和 Python 获取 OAuth 凭证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14061599/

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