pythongoogle-api-python-clientoauth2clientgoogle-genomics

How to add client_secret without using argparser?


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...


Solution

  • 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