【问题标题】:Google Sheets API json files - What is the difference between CLIENT_SECRET and oauth2client credentials?Google Sheets API json 文件 - CLIENT_SECRET 和 oauth2client 凭据有什么区别?
【发布时间】:2018-06-10 09:02:30
【问题描述】:

我遵循了 Google Sheet Python API 快速入门指南 (https://developers.google.com/sheets/api/quickstart/python) 并能够使用他们提供的代码使其工作:

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """

    # If modifying these scopes, delete your previously saved credentials
    # at ~/.credentials/sheets.googleapis.com-python-quickstart.json
    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
    CLIENT_SECRET_FILE = 'my/path/client_secret.json'
    APPLICATION_NAME = 'Google Sheets API Python Quickstart'

    credential_path = 'my/path/sheets.googleapis.com-python-quickstart.json'

    store = Storage(credential_path)
    credentials = store.get()

    ## !!!!! Is this needed?
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)

    return credentials

在默认设置中,我下载了两个 JSON 文件:

  • client_secret.JSON
    • 下载到project目录。
  • sheets.googleapis.com-python-quickstart.JSON
    • 下载到~/.credentials目录

sheets.googleapis.com JSON 文件开头为:

"_module": "oauth2client.client".

问题 1:每个 JSON 文件的用途是什么?

问题 2: 是否需要这两个 JSON 文件才能成功使用 Google Sheets API?

  • 我不认为,因为我可以在没有 client_secret.JSON 文件的情况下使 API 正常工作。

【问题讨论】:

    标签: python json api credentials google-sheets-api


    【解决方案1】:

    这个答案怎么样?我想当你知道 OAuth2 获取访问令牌和刷新令牌的过程后,你就可以理解这两个文件的含义了。使用 OAuth2 获取访问令牌和刷新令牌的流程如下。

    流程:

    1. 从 API 控制台下载 client_secret.JSON
      • client_secret.JSON 包括client_idclient_secretredirect_uris
    2. 使用范围和client_idclient_secret.JSON 检索授权代码。
    3. 使用授权码client_idclient_secretredirect_uris检索访问令牌和刷新令牌。
      • 检索到的访问令牌、刷新令牌等参数保存到sheets.googleapis.com-python-quickstart.JSON的文件中。

    注意:

    • 当您第一次运行快速入门时,会启动使用浏览器的授权过程。此时,Quickstart 的脚本使用client_id 和范围检索授权码,然后使用授权码client_idclient_secretredirect_uris 检索访问令牌和刷新令牌。
    • 在快速入门第一次运行后,访问令牌由来自sheets.googleapis.com-python-quickstart.JSON 的刷新令牌检索。这样,就不需要使用浏览器检索授权码了。因此,当有sheets.googleapis.com-python-quickstart.JSON 时,不需要client_secret.JSON
      • 我认为这可以回答您的问题 2。
    • 但是,如果您想更改client_secret.JSON 的范围和/或凭据,则需要使用浏览器进行授权过程并检索授权码。为此,您必须删除sheets.googleapis.com-python-quickstart.JSON 并再次授权。当时,在 Quickstart 中,client_secret.JSON 再次被使用。

    参考资料:

    如果这对你没有用,我很抱歉。

    【讨论】:

    • 非常有帮助。我的理解是,如果我想在另一台计算机上运行我的应用程序,我只需要发送我的 'client_secret.json' 文件,因为仅该文件就可以让我生成一个新的 'sheets.googleapis.com-python- quickstart.JSON' 文件在还没有该文件的新计算机上。对吗?
    • 是否有理由不将 sheet.googleapis.com-python-quickstart.JSON 文件保存在与 client_secret.json 相同级别的项目目录中?又名:我是否有理由需要设置此逻辑以在我拥有它一次后生成它?
    • @ben 你可以把client_secret.json放到你想要的目录下。但请在脚本中设置路径。在检索刷新令牌时读取它。因此,当您已经拥有刷新令牌时,它不会用于运行脚本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2014-02-27
    • 1970-01-01
    • 2018-10-27
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多