【问题标题】:How can I use Python Google API without getting a fresh auth code via browser each time?如何使用 Python Google API 而无需每次都通过浏览器获取新的身份验证代码?
【发布时间】:2018-07-23 21:01:29
【问题描述】:

我正在使用 Google API。我以this 为起点,here 是实际的 Python 代码。

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

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"
    ]
  }
}

整个程序运行正常,成功返回了一个有意义的数据集,但是每次运行程序都会提示如下:

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 的存在正是为了避免这种必要性。

我必须做些什么才能让我的 CLI Python 程序使用 API 而不必每次都获取新的令牌?

【问题讨论】:

  • 我的回答是否向您展示了您想要的结果?你能告诉我吗?这对我学习也很有用。如果这可行,与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果您对我的回答有疑问,请随时告诉我。我想学习解决你的问题。
  • 是的,这可以作为答案。我并不完全满意,因为它看起来像是一个 hack,而且因为看起来客户端机密文件应该是这样做的,这意味着我/我们可能仍然没有以正确的方式去做。但它有效。谢谢。
  • 感谢您的回复。虽然我不确定我是否能正确理解您的情况,但使用 Service Accounts 怎么样?该文件是here。如果我误解了你的情况,我很抱歉。

标签: google-api


【解决方案1】:

您希望在不每次都检索代码的情况下运行脚本。如果我的理解是正确的,那么这个修改呢?在此修改中,第一次运行脚本时,刷新令牌被保存到“credential_sample.json”文件中。这样,从下一次运行开始,您就可以使用通过刷新令牌检索到的访问令牌来使用 API。因此,您无需每次都检索代码。

修改后的脚本:

请进行如下修改。

从 :
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)
到 :
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”。
  • 此修改假设您当前的脚本除了每次都需要检索代码之外都可以正常工作。

参考资料:

如果这不是你想要的,我很抱歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 2018-08-14
    • 2013-11-11
    • 2016-08-03
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多