Search code examples
python-2.7google-oauthgoogle-api-python-clientgmail-api

run_flow complains about getting its minimum three arguments


I'm working on a simple script to send emails via the GMail API, and an old script I found to access their SMTP interface wasn't working.

So I used the following script from their quickstart page to start first with reading:

#! /usr/bin/env python
#
                                                                                                                                                               
import httplib2
                                                                                                                                                               
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run

CLIENT_SECRET = '.client.json'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'
STORAGE = Storage('gmail.storage')
                                                                                                                                                               
flow = flow_from_clientsecrets(CLIENT_SECRET, scope=OAUTH_SCOPE)
http = httplib2.Http()
                                                                                                                                                               
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
    credentials = run(flow, STORAGE, http=http)
                                                                                                                                                                   
http = credentials.authorize(http)
                                                                                                                             
gmail_service = build('gmail', 'v1', http=http)
threads = gmail_service.users().threads().list(userId='me').execute()
                                                                                                                                                                   
if threads['threads']:
    for thread in threads['threads']:
        print 'Thread ID: %s' % (thread['id'])

Running this gives a NotImplementedError as shown in this question.

So I imported and called run_flow instead of run, as I did not install gflags to continue. However, I get the following error:

TypeError: run_flow() takes at least 3 arguments (3 given)

I understand from the linked question that argparse should help. I could add the call to parser that that question uses, but I would have no idea what arguments to pass on the command line.

Anyone successfully implemented something with this who could give some help?


Solution

  • You don't need to pass extra arguments to the command line when using run_flow python.

    import argparse
    ...
    from oauth2client import tools
    ...
    from oauth2client.tools import run_flow
    ...
    parser = argparse.ArgumentParser(parents=[tools.argparser])
    flags = parser.parse_args()
    ....
    credentials = run_flow(flow, STORAGE, flags, http=http)
    

    Then you can run

    python quickstart.py