python - InstalledAppFlow 自动获取授权码

标签 python google-authentication google-api-python-client

我正在使用从 google_auth_oauthlib.flow 导入的 InstalledAppFlow。每次运行代码时,我都必须手动获取授权码。代码将如下所示:

请访问此 URL 以授权此应用程序:https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=929791903032-hpdm8djidqd8o5nqg2gk636efau34ea6q.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg %3Aoauth%3A2.0%3Aoob&scope=https%3A%22F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics-monetary.readonly&state=a0drph4tJFDc2rIGtdwUStIGbRKNnkv18&prompt=consent&access_type=offline

输入授权码: 然后我必须点击上面的 accounts.google.com 输入授权码然后​​粘贴它: 4/0gCyNaDZeNRr5pnvkXFO1HvLjsq5IbjVYjETaUtlq8_1PqoaY9totzs

我的问题是如何自动而不是手动执行此步骤?

最佳答案

就像 tehhowch 说的,他们的自动化这个过程的文档使用:

flow.run_local_server()

而不是:

flow.run_console()

然后在下次运行时自动执行:

flow.run_local_server()
with open('token.pickle', 'wb') as token:
    pickle.dump(credentials, token)

它应该在第一次运行后没有提示的情况下工作。

https://developers.google.com/apps-script/api/quickstart/python

关于python - InstalledAppFlow 自动获取授权码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54175158/

相关文章:

python - 相机标定opencv python

python - 使用 MySQLdb 执行多个 SQL 查询

google-api - Google API 刷新 token 不接受 'incremental scope' 请求

python - GAE 中的 OAuth2 身份验证访问日历 API V3(域托管)

python - Google Calendar API 事件总是减去一天

python - 在 subprocess.call() 中使用带反引号的命令替换

google-apps - 将 Google 帐户登录限制到指定的 Apps 域

node.js - 相同的 Google Cloud Storage 上传脚本可以在一台 PC 上运行,但在另一台 PC 上则不行,为什么?

oauth-2.0 - 我收到此错误 :- Error 400: redirect_uri_mismatch even after I registered the URI in Google Gmail API OAuth 2. 0

python - 将英尺转换为 CM