开发者

issue with gdata analytics client in python

I was able to successfully retrieve an OAuth access token for Google Analytics using Google's gdata python library.

However, my attempt to use the token to access Google Analytics data is failing. Here is the relevant code snippet:

client = gdata.analytics.client.AnalyticsClient(source='myapp')

client.auth_token = access_token # retrieved earlier

dataQuery = gdata.analytics.client.DataFeedQuery({
    'ids': 'ga:********',
    'start-date': '2011-03-23',
    'end-date': '2011-04-04',
    'metrics': 'ga:percentNewVisits',
    'max-results': 50})

data = client.GetDataFeed(dataQuery)

I get the following stacktrace:

Traceback (most recent call last):

File "/Library/Python/2.6/site-packages/django/core/servers/basehttp.py", line 280, in run self.result = application(self.environ, self.start_response)

File "/Library/Python/2.6/site-packages/django/core/servers/basehttp.py", line 674, in call return self.application(environ, start_response)

File "/Library/Python/2.6/site-packages/django/core/handlers/wsgi.py", line 248, in call response = self.get_response(request)

File "/Library/Python/2.6/site-packages/django/core/handlers/base.py", line 141, in get_response return self.handle_uncaught_exception(request, resolver, sys.exc_info())

File "/Library/Python/2.6/site-packages/django/core/handlers/base.py", line 100, in get_response response = callback(request, *callback_args, **callback_kwargs)

File "/Library/Python/2.6/site-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view return view_func(request, *args, **kwargs)

File "/Users/***/***/**/***/**/googleAnalyticsOauth.py", line 122, in googleAnalyticsTest data = client.GetDataFeed(dataQuery)

File "build/bdist.macosx-10.6-universal/egg/开发者_运维知识库gdata/analytics/client.py", line 77, in get_data_feed **kwargs)

File "build/bdist.macosx-10.6-universal/egg/gdata/client.py", line 635, in get_feed **kwargs)

File "build/bdist.macosx-10.6-universal/egg/gdata/client.py", line 265, in request uri=uri, auth_token=auth_token, http_request=http_request, **kwargs)

File "build/bdist.macosx-10.6-universal/egg/atom/client.py", line 110, in request self.auth_token.modify_request(http_request)

File "build/bdist.macosx-10.6-universal/egg/gdata/gauth.py", line 980, in modify_request token_secret=self.token_secret, verifier=self.verifier)

File "build/bdist.macosx-10.6-universal/egg/gdata/gauth.py", line 604, in generate_hmac_signature next, token, verifier=verifier)

File "build/bdist.macosx-10.6-universal/egg/gdata/gauth.py", line 565, in build_oauth_base_string urllib.quote(params[key], safe='~')))

File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", line 1216, in quote res = map(safe_map.getitem, s)

TypeError: argument 2 to map() must support iteration

Anyone have any ideas what could be going wrong?

Thanks!


The problem is in gauth.py (part of the gdata client library) at about line 587 in version 2.0.15.

The value for the "max-results" parameter being passed to urllib.quote is 10000, an integer, not a string, so it doesn't have an iterator.

My quick hacky fix is:

  for key in sorted_keys:
    safe_str_param = urllib.quote(str(params[key]), safe='~')
    pairs.append('%s=%s' % (urllib.quote(key, safe='~'), safe_str_param))

You can track down the problem yourself by using pdb, like this:

python -m pdb pagination_demo.py
> ga-api-http-samples-read-only/src/data_export/v2/python/pagination/pagination_demo.py(35)<module>()
-> """ """
# Note that (Pdb) indicates a prompt from the debugger
(Pdb) c
Executing query: https://www.google.com/analytics/feeds/data?max-results=10000&...&start-date=2011-01-01&ids=ga%3A999999&metrics=ga%3Apageviews&end-date=2011-12-30

# then you get more or less your trace above, plus this:

TypeError: argument 2 to map() must support iteration
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> lib/python2.6/urllib.py(1224)quote()
-> res = map(safe_map.__getitem__, s)

# Ok, let's see what the type of 's' is with pretty print
(Pdb) pp type(s)
<type 'int'>
(Pdb) q

If everything works (like when you add my hack above to gauth.py), you'll see this instead of the stack trace:

Total results found: 124
Total pages needed, with one page per API request: 1

The program finished and will be restarted
> ga-api-http-samples-read-only/src/data_export/v2/python/pagination/pagination_demo.py(35)<module>()
-> """
(Pdb) q


FWIW, one has to do the following:

my_client.auth_token = gdata.gauth.OAuthHmacToken(CONSUMER_KEY, CONSUMER_SECRET, TOKEN, TOKEN_SECRET, gdata.gauth.ACCESS_TOKEN)

Via

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜