Search code examples
pythonpandasgoogle-analyticsgoogle-apigoogle-analytics-api

Pandas / Google Analytics API authentication attempt throws me a weird python error


During my pandas/Google Analytics API setup, I basically did everything as described in this link:

http://blog.yhathq.com/posts/pandas-google-analytics.html

The client_secrets.json is in the pandas/io folder. When i now try to execute a statement of the form

>>>from pandas.io import ga
>>>df = ga.read_ga(metrics, dimensions, start_date)

the following error occurs:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "\Anaconda\lib\site-packages\pandas\io\ga.py", line 110, in read_ga
    reader = GAnalytics(**reader_kwds)
  File "\Anaconda\lib\site-packages\pandas\io\ga.py", line 179, in __init__
    self._service = self._init_service(secrets)
  File "\Anaconda\lib\site-packages\pandas\io\ga.py", line 191, in _init_service
    http = self.authenticate(secrets)
  File "\Anaconda\lib\site-packages\pandas\io\ga.py", line 151, in authenticate
    return auth.authenticate(flow, self.token_store)
  File "\Anaconda\lib\site-packages\pandas\io\auth.py", line 108, in authenticate
    credentials = tools.run(flow, storage)
AttributeError: 'module' object has no attribute 'run'

According to the yhat link, my browser should open for authentication.

Note: I did not not create the Client ID for "installed application", since I did not have this choice in the menu when creating the ID. Instead, i chose "other". This shouldn't be the cause of the error, though.

Second Note: I recently updated my pandas to 0.17.1. When importing pandas.io.ga, i got the message that the .ga module is deprecated. Furthermore, i manually installed the gflags module, because it was needed when I tried to import .io.ga the first time.


Solution

  • Either file a ticket with the owners of Pandas to change (currently) line 108 of pandas/io/auth.py from run() to run_flow(), or make the fix yourself and file a PR. (Yes, it would've been nice if Google had just made run_flow() and alias of run(), but as you can imagine, this is not how this change evolved, so we have to live with it.)

    For other developers running into this error: If you have the latest version (as of Feb 2016) of the Google APIs Client Library for Python, just rename your call from tools.run() to tools.run_flow(), and you should be good-to-go. More about this change in a PSA (public service announcement) blogpost I wrote back in mid-2015 but update periodically to stay current.

    The fastest way to upgrade your Client Library is with: pip install -U google-api-python-client # or pip3 for 3.x