如何在不每次都通过浏览器获取新的身份验证代码的情况下使用Python Google API? [英] How can I use Python Google API without getting a fresh auth code via browser each time?

查看:74
本文介绍了如何在不每次都通过浏览器获取新的身份验证代码的情况下使用Python Google API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google API.我使用作为起点,

I am playing with the Google API. I am using this as a starting point, here is the actual Python code.

我已经在 https://console.developers.google.com上创建了OAuth 2.0客户端ID./apis/credentials 并将其下载为 client_secret.json ,其在代码中的用法如下:

I have created a OAuth 2.0 client ID at https://console.developers.google.com/apis/credentials and downloaded it as client_secret.json which is used in the code as follows:

CLIENT_SECRETS_FILE = "client_secret.json"

def get_authenticated_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  credentials = flow.run_console()
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

client_secret.json 的内容如下:

{
  "installed": {
    "client_id": "**REDACTED**",
    "project_id": "api-project-1014650230452",
    "auth_uri": "https:\/\/accounts.google.com\/o\/oauth2\/auth",
    "token_uri": "https:\/\/accounts.google.com\/o\/oauth2\/token",
    "auth_provider_x509_cert_url": "https:\/\/www.googleapis.com\/oauth2\/v1\/certs",
    "client_secret": "**REDACTED**",
    "redirect_uris": [
      "urn:ietf:wg:oauth:2.0:oob",
      "http:\/\/localhost"
    ]
  }
}

整个程序可以正常工作并成功返回有意义的数据集,但是,每次我运行该程序时,都会提示如下:

The whole program works and successfully returns a meaningful data set, however, each time I run the program I am prompted as follows:

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=...
Enter the authorization code:

我输入了代码,程序正常运行,但是每次程序运行时,我都必须访问此URL并获取新的代码.我的印象是 client_secret.json 的存在正是为了防止这种情况的发生.

I enter the code and the program works, but I have to visit this URL and get a fresh code each time the program runs. I was under the impression that client_secret.json existed precisely to prevent this from being necessary.

要使我的CLI Python程序使用API​​而不必每次都获取新令牌,该怎么办?

What do I have to do to make my CLI Python program use the API without having to get a fresh token each time?

推荐答案

您要运行脚本而不每次都检索代码.如果我的理解是正确的,那么该修改如何?在此修改中,首次运行脚本时,刷新令牌将保存到文件"credential_sample.json".这样,在下一次运行中,您可以使用由刷新令牌检索到的访问令牌来使用API​​.因此,您不必每次都检索代码.

You want to run the script without retrieving the code every time. If my understanding is correct, how about this modification? In this modification, when the script is run for the first time, the refresh token is saved to a file of "credential_sample.json". By this, from the next run, you can use the API using the access token retrieved by the refresh token. So you are not required to retrieve the code every time.

请进行如下修改.

def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
    credentials = flow.run_console()
    return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

至 :从oauth2client导入客户端的

To :

from oauth2client import client # Added
from oauth2client import tools # Added
from oauth2client.file import Storage # Added

def get_authenticated_service(): # Modified
    credential_path = os.path.join('./', 'credential_sample.json')
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRETS_FILE, SCOPES)
        credentials = tools.run_flow(flow, store)
    return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)

注意:

  • 首次运行此程序时,浏览器将自动打开.授权范围时,脚本会自动检索代码并为"credential_sample.json"创建令牌.
  • 此修改假设您的当前脚本可以正常工作,只是每次都需要检索代码.
    • Python快速入门
      • 关于此修改,您也可以参考此脚本.

      如果这不是您想要的,对不起.

      If this was not what you want, I'm sorry.

      这篇关于如何在不每次都通过浏览器获取新的身份验证代码的情况下使用Python Google API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆