0

我想测试Google Genomics。我有一个项目,我可以从getting started with the api运行main.py。但这个文件皮张oauth2client的引擎盖下的凭证是如何产生的:如何在不使用argparser的情况下添加client_secret?

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) 

有人能解释我这是什么码?我怎么能把它转换成没有争论的东西?

我试着用google-api文档的其他解决方案,但主要是我不明白正在做什么,所以我不明白我该怎么做。 (我也不完全了解OAuth2client) This answer建议argparse是强制性的。但this其他方式使用google-api-python-client不要使用它...

回答

0

如果你想要的话,你可以使用API​​密钥,这在实现服务器时更实用 - 尽管你不想与任何人共享。下面是描述的oauth2协议是如何工作的,以谷歌的API提供接入两大环节:

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

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

希望它能帮助,

保罗

+0

感谢保罗,我结束了与'服务=构建('基因组','v1beta2',developerKey = api_key)' – Llopis

+0

API密钥伟大 - 你不会后悔:)是的使用API​​密钥让我们通常集中更多的分析或代码。 –

0

argparse的目的是解析命令行选项。如果您打算在命令行中使用参数,则argparse比没有参数容易得多。

如果您想要硬编码参数(或以某种其他方式检索它们),则可以将所有parser行撕掉,并用适当的值替换flags变量(例如,对于客户机密文件名) 。

+0

我要硬编码的参数,但你能告诉我如何正确地取代'fl​​ags'变量?感谢您的回答 – Llopis

+0

用硬编码值替换'flags.client_secrets_filename'。 – Kevin

相关问题