Search code examples
pythonshelloauth-2.0google-oauthgoogle-api-python-client

Error: "unrecognized arguments: shell" - OAuth 2.0 Google APIs Client for Python


I'm trying to do a pure server side authentication. I've followed all the procedures mentioned in the code sample provided on google developers. The problem is that execfile() works fine in the normal python shell but it throws the below mentioned error in the python interactive shell.

Following is the log for the statements. What else needs to be defined in the playlistitems.py file so that it can be executed in the Python Interactive Shell.

Code for playlistitems.py : Sample Code

Kshitij:trailers_backend Kshitij$ python manage.py shell
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> execfile("playlistitems.py")
usage: manage.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}]
manage.py: error: unrecognized arguments: shell
Kshitij:trailers_backend Kshitij$ 

I passed the arguments by changing the values in the tools.py itself. Following is the error that I got. How do I solve it? And end to end example for this case?

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/Kshitij/django project/trailers_backend/videoStore/getVideos.py", line 62, in getVideos
     credentials = run_flow(flow, storage, flags)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1260, in add_argument
    if not args or len(args) == 1 and args[0][0] not in chars:
TypeError: 'bool' object has no attribute '__getitem__'
>>> 

Solution

  • I finally got the working code with some help from @Stormie. So here is the working code snippet.

    if credentials is None or credentials.invalid:
        flags = argparser.parse_args('--auth_host_name localhost --logging_level INFO'.split())
        credentials = run_flow(flow, storage, flags)
    

    I got to know about parse_args() just now. parse_args() method of ArgumentParser is needed to be passed the parameters.

    If you are authenticating from the command line and your browser doesn't support Javascript, you may want to add the argument --noauth_local_webserver:

    flags = argparser.parse_args('--auth_host_name localhost --logging_level INFO --noauth_local_webserver'.split())
            credentials = run_flow(flow, storage, flags)