3

I have a flask application where I can run a script (with the help of Flask-script) that makes use of google api discovery using the code below:

app_script.py

import argparse
import csv
import httplib2

from apiclient import discovery
from oauth2client import client
from oauth2client.file import Storage
from oauth2client import tools


def get_auth_credentials():
  flow = client.flow_from_clientsecrets(
    '/path/to/client_screts.json',  # file downloaded from Google Developers Console
    scope='https://www.googleapis.com/auth/webmasters.readonly',
    redirect_uri='urn:ietf:wg:oauth:2.0:oob')

  storage = Storage('/path/to/storage_file.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
      parser = argparse.ArgumentParser(parents=[tools.argparser])
      flags = parser.parse_args(['--noauth_local_webserver'])
      credentials = tools.run_flow(flow=flow, storage=storage, flags=flags)

  return credentials

def main():
  credentials = get_auth_credentials()
  http_auth = credentials.authorize(httplib2.Http())
  # build the service object
  service = discovery.build('webmasters', 'v3', http_auth)

Now the problem is every time I shutdown my computer upon booting and running the script again, I get the following error when trying to build the service object:

terminal:

$ python app.py runscript
No handlers could be found for logger "oauth2client.util"
Traceback (most recent call last):
  File "app.py", line 5, in <module>
    testapp.manager.run()
  File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/home/user/development/testproject/testapp/__init__.py", line 16, in runscript
    metrics_collector.main()
  File "/home/user/development/testproject/testapp/metrics_collector.py", line 177, in main
    service = discovery.build('webmasters', 'v3', http_auth)
  File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 206, in build
credentials=credentials)
  File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 306, in build_from_document
    base = urljoin(service['rootUrl'], service['servicePath'])
KeyError: 'rootUrl'

intalled:

google-api-python-client==1.4.2
httplib2==0.9.2
Flask==0.10.1
Flask-Script==2.0.5

The script runs sometimes*, but thats the problem I don't know why it runs sometimes and others doesn't

*What I tried to make it work was to, delete all the cookies, download the client_secrets.json from the Google Developers Console again, remove the storage_file.dat, remove all .pyc files from the project

Can anyone help me see what's going on?

PMigC
  • 91
  • 1
  • 7

2 Answers2

1

From a little bit of research here, it seems that the No handlers could be found for logger "oauth2client.util" error can actually be masking a different error. You need to use the logging module and configure your system to output.

Solution

Just add the following to configure logging:

import logging
logging.basicConfig()

Other helpful/related posts

Python - No handlers could be found for logger "OpenGL.error"

SOLVED: Error trying to access "google drive" with python (google quickstart.py source code)

Community
  • 1
  • 1
Avantol13
  • 1,009
  • 11
  • 21
0

Thank you so much for the tip Avantol13, you were right there was an error being masked.

The problem was that the following line:

service = discovery.build('webmasters', 'v3', http_auth)

should have actually be:

service = discovery.build('webmasters', 'v3', http=http_auth)

All working now. Thanks

PMigC
  • 91
  • 1
  • 7