0

I want to test Google Genomics. I have a project and I can run main.py from the getting started with the api. But this files hides under the hood of oauth2client how credentials are generated:

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)

Can somebody explain me what does the code? How could I convert that into something without argparse?

I tried with other solutions of google-api documentation but the main point is that I don't understand what is being done, so I can't understand what I should do. (I also don't fully understand OAuth2client) This answer suggest that argparse is mandatory. But this other way using google-api-python-client don't use it...

Community
  • 1
  • 1
llrs
  • 3,308
  • 35
  • 68

2 Answers2

0

The purpose of argparse is to parse command line options. If you plan on taking arguments at the command line, it's much easier with argparse than without.

If you want to hard-code the arguments (or retrieve them in some other manner), you can just rip out all the parser lines, and replace the flags variable with the appropriate values (e.g. for the client secrets filename).

Kevin
  • 28,963
  • 9
  • 62
  • 81
  • I want to hard-code the argument, but could you please show me how to properly replace the `flags` variable? Thanks for the answer – llrs Apr 17 '15 at 14:35
  • Replace `flags.client_secrets_filename` with the hard-coded value. – Kevin Apr 17 '15 at 14:35
0

If you want you can use an API key instead, which is more practical when implementing servers - though you would not want to share it with anyone. Below are two great links that describe how the Oauth2 protocol works in providing access to Google's APIs:

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

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

Hope it helps,

Paul

Paul Grosu
  • 61
  • 2
  • Thanks Paul, I ended up using the API key with `service = build('genomics', 'v1beta2', developerKey=api_key)` – llrs Apr 21 '15 at 12:21
  • Great - you won't regret it :) Yeah using the API key let's me usually focus more at the analysis or code. – Paul Grosu Apr 21 '15 at 15:43