2

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__'
>>> 
1
  • You should also include the actual code of playlistitems.py, it would help us highlight what is missing in your code. Commented Aug 27, 2014 at 14:23

3 Answers 3

5

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)
Sign up to request clarification or add additional context in comments.

Comments

3

This worked for me from oauth2client import tools flags = tools.argparser.parse_args([]) This prompted me to authentication url in browser.Hope this helps

Comments

0

You need to pass through arguments for your run_flow() method from the OAuth2Client tools. It shows that when you call run_flow() you must parse the following arguments:

run_flow(flow, storage, flags, http=None)

The flags are what you are missing, you must add the flags (described in the link):

--auth_host_name: Host name to use when running a local web server
    to handle redirects during OAuth authorization.
    (default: 'localhost')

--auth_host_port: Port to use when running a local web server to handle
    redirects during OAuth authorization.;
    repeat this option to specify a list of values
    (default: '[8080, 8090]')
    (an integer)

--[no]auth_local_webserver: Run a local web server to handle redirects
    during OAuth authorization.
    (default: 'true')

I still haven't cracked how exactly to parse them unfortunately, I currently have a bountied question regarding how to emulate the old run() method of authenticating your session here.

2 Comments

Hey Thanks for your reply. I've updated the argparser in tools.py with default values. (I'm not sure if I'm doing it right). Nonetheless, I got an error which I've updated in the question. Can you please help me with this end to end?
Hi, I somehow got it working. Your instructions helped. But I don't clarity as to how do I pass multiple arguments in parse_args()? Even this link doesn't give an example for passing multicharacter options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.