Overwriting Google Drive API v3 Argparse in Python
<p>I'm trying to use the Google Drive API (v3) with Python to obtain and upload files to my Google Drive account. </p>

<p>I used this guide to setup my authentication: <a href="https://developers.google.com/drive/v3/web/quickstart/python" rel="nofollow">https://developers.google.com/drive/v3/web/quickstart/python</a></p>

<p>But for my program, I would like to take commandline input for username, filename, and output_filename. I modified the google doc code and did the following:</p>

<pre><code>  from __future__ import print_function
    import httplib2
    import os
    from sys import argv 
    from apiclient import discovery
    from oauth2client import client
    from oauth2client import tools
    from oauth2client.file import Storage
    from apiclient.http import MediaIoBaseDownload, MediaIoBaseUpload 
    import io  

    try:
        import argparse
        parser = argparse.ArgumentParser(description="I want your name, the file ID, and the folder you want to dump output to")
        parser.add_argument('-u', '--username', help='User Name', required=True)
        parser.add_argument('-f', '--filename', help='File Name', required=True)
        parser.add_argument('-d', '--dirname', help = 'Directory Name', required=True)
        flags = parser.parse_args()

    except ImportError:
        flags = None

    SCOPES = 'https://www.googleapis.com/auth/drive'
    CLIENT_SECRET_FILE = 'client_secret.json'
    APPLICATION_NAME = 'Drive API Python Quickstart'
    ...#rest of the code is from the Google Drive Documentation (see above)

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

    home_dir = os.path.expanduser('~')

    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'drive-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    #Credentials returns NONE
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if args:
          credentials = tools.run_flow(flow, store)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)

    print("check")
    return credentials
</code></pre>

<p>The problem is that in the get_credentials method, there is a line that says:</p>

<pre><code>if flags:
  credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
  credentials = tools.run(flow, store)
</code></pre>

<p>The run_flow method though uses a different argparser that google wrote (see: <a href="http://oauth2client.readthedocs.io/en/latest/source/oauth2client.tools.html" rel="nofollow">http://oauth2client.readthedocs.io/en/latest/source/oauth2client.tools.html</a>)</p>

<p>So whenever I run this script with my own inputs with username, filename, etc. I keep getting an error that says "Unrecognized Arguments". </p>

<p>How do I make my argparser over-write the one in run_flow?</p>

<p>EDIT:</p>

<p>Someone suggested using parse_known_args().</p>

<p>Well, I modified my code to parse by saying args, flags = parser.parse_known_args() because that way, any misc. inputs would go into flags. </p>

<p>The idea is that if I run the script and give it my 3 args, it should pull them into "args". </p>

<p>But the problem with this again is that later on when I call the run_flow method in get_credentials, it throws an error saying:</p>

<blockquote>
  <p>Usage: name.py [--auth_host_name AUTH_HOST_NAME]
                 [--noauth_local_webserver]
                 [--auth_host_port [AUTH_HOST_PORT ...]]]
                 [--logging_level {DEBUG, INFO, WARNING, ERROR, CRITICAL}] Unrecognized arguments: -u shishy -f fname -d random_name</p>
</blockquote>

<p>I think it's still passing my command line input to the get_info method and the parser there has no idea what to do with it...</p>

<p>Nevermind, I figured it out. hpaulj had it right.</p>

<p>In the get_credentials() method, right before it called run_flow(), I simply had to add a line saying:</p>

<pre><code>flags=tools.argparser.parse_args(args=[])
credentials=tools.run_flow(flow, store, flags)
</code></pre>

<p>And at the beginning when I read the command line with my inputs, I simply used parser_known_flags() as hpaulj suggested!</p>

<p>Thanks,
shishy</p>

python google-drive-sdk argparse oauth2