如何在不使用 argparse 的情况下添加 client_secret?

How to add client_secret without using argparser?

我想测试 Google 基因组学。我有一个项目,我可以 运行 main.py from the getting started with the api。但是这个文件隐藏在 oauth2client 的幕后是如何生成凭据的:

import argparse
import httplib2
from apiclient.discovery import build
from collections import Counter
from oauth2client import tools
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow

# For these examples, the client id and client secret are command-line arguments
parser = argparse.ArgumentParser(description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=[tools.argparser])
parser.add_argument('--client_secrets_filename',
                    default='client_secrets.json',
                    help='The filename of a client_secrets.json file from a '
                         'Google "Client ID for native application" that '
                         'has the Genomics API enabled.')
flags = parser.parse_args()

# Authorization
storage = Storage('credentials.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
  flow = flow_from_clientsecrets(
    flags.client_secrets_filename,
    scope='https://www.googleapis.com/auth/genomics',
    message='You need to copy a client_secrets.json file into this directory, '
            'or pass in the --client_secrets_filename option to specify where '
            'one exists. See the README for more help.')
  credentials = run_flow(flow, storage, flags)

# Create a genomics API service
http = httplib2.Http()
http = credentials.authorize(http)

有人可以解释一下代码是什么吗?我怎么能把它转换成没有 argparse 的东西呢?

我尝试了 google-api 文档的其他解决方案,但要点是我不明白正在做什么,所以我不明白我应该做什么。 (我也不完全理解 OAuth2client) This answer suggest that argparse is mandatory. But this 其他方式使用 google-api-python-客户端不要使用它...

argparse的目的是解析命令行选项。如果您计划在命令行中获取参数,使用 argparse 比不使用要容易得多。

如果你想对参数进行硬编码(或以其他方式检索它们),你可以删除所有 parser 行,并将 flags 变量替换为适当的值(例如,客户端机密文件名)。

如果您愿意,可以改用 API 密钥,这在实施服务器时更实用 - 尽管您不想与任何人共享。下面是两个很棒的链接,它们描述了 Oauth2 协议如何提供对 Google 的 APIs 的访问:

https://developers.google.com/identity/protocols/OAuth2

https://developers.google.com/identity/protocols/OAuth2WebServer

希望对您有所帮助,

保罗