0

I have a python (2.7) program that uses oauth2client to access google drive. I'm trying to get my initial credentials file. I'm using this:

from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run_flow


    SCOPE = 'https://www.googleapis.com/auth/drive'
    credentials = 'https://www.googleapis.com/auth/drive'
    CLIENT_SECRET_FILE = os.path.join(credential_path, 'client_secret.json'
    flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=SCOPE)
    http = httplib2.Http()
    credentials = run_flow(flow, store, http=http)

However, I am getting what appears to be an Argparser usage error?

usage: responseratedash2.py [--auth_host_name AUTH_HOST_NAME]
                            [--noauth_local_webserver]
                            [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                            [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
responseratedash2.py: error: unrecognized arguments: -r 2341 -t

responseratedash2.py is the name of my program, and -r 2341 -t are valid arguments for that program. But this error is happening in the run_flow line...

Why is this happening?

(ETA: Explaining how I'm using oauth2client, the flow functions are both from that library.)

Brian Postow
  • 11,709
  • 17
  • 81
  • 125
  • It appears tat `responseratedash2` has its own parser, and is using the same `sys.argv` that your own code is using. So while the `-r` may be valid for your code, it isn't valid for that parser. You can see what arguments it recognizes, all those login values. – hpaulj Jan 11 '19 at 17:55
  • Sure, but why is any library that lots of people use using sys.argv??? How do I make it not assume that the surrounding program doesn't have it's own arguments? – Brian Postow Jan 11 '19 at 18:07
  • Oh, and responseratedash2 is the name of my program. The usage listed is **NOT** the correct usage for responseratedash2. -r and -t are fire for responseratedash2. – Brian Postow Jan 11 '19 at 18:08
  • How is `oauth2client` supposed to get those arguments? Is your own code ok with those? Is it prepared to pass them on to a relevant module or function. I've seen an issue like this before, but don't recall the solution. I suspect `oautho2client` (or what ever it's called) is meant to be called as a script, not as an embedded module. You don't show anything about how you invoke it. – hpaulj Jan 11 '19 at 18:13
  • Possibly relevant: https://stackoverflow.com/questions/46737536/unrecognized-arguments-using-oauth2-and-google-apis – hpaulj Jan 11 '19 at 18:16
  • and: https://stackoverflow.com/questions/24890146/how-to-get-google-analytics-credentials-without-gflags-using-run-flow-instea and https://developers.google.com/api-client-library/python/guide/aaa_oauth#command-line-tools – hpaulj Jan 11 '19 at 18:24
  • I would assume that a library gets arguments via function parameters not sys.argv. – Brian Postow Jan 11 '19 at 19:55
  • All usage of oauth2client I've seen have been as library, but assuming command line args for some reason... – Brian Postow Jan 11 '19 at 20:00

1 Answers1

1

The answer appears to be, for some reason, oauth2client.tools assumes that you're running it with command line arguments. There is probably a good reason for this for the original use-case, but it looks really weird for mine.

The solution is that run_flow takes a flags argument, which can be set up via:

flags=oauth2client.tools.argparser.parse_args(args=[])

which zeros that out...

Brian Postow
  • 11,709
  • 17
  • 81
  • 125
  • Yes, the first thing the function code does is check if `flags is None`, in which case it sets it with `flags=argparser.parse_args()`. I'd suggest looking at the `flags` variable that your code ran. Running `parse_args` with `[]` is equivalent to running the script without any commandline values. – hpaulj Jan 11 '19 at 21:03
  • My point is that running parse_args with [] is THE ONLY WAY to run the library function as if it has no commandline args... (I tried passing [] as flags, to run_flow and it didn't appear to work...) – Brian Postow Jan 17 '19 at 20:29
  • Right, `flags` is supposed to be a `argparse.Namespace` with the right attributes. Or a functional equivalent. The `Namespace` class isn't complicated. It's just looking for the 3 documented attributes. Using `args=[]` produces flags with the documented defaults. – hpaulj Jan 17 '19 at 20:44