我能够使用 Google 的 gdata python 库为 Google Analytics 成功检索 OAuth 访问 token 。
但是,我尝试使用 token 访问 Google Analytics 数据的尝试失败了。这是相关的代码片段:
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)
我得到以下堆栈跟踪:
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
任何人都有任何想法可能会出现什么问题?
谢谢!
最佳答案
问题出在 2.0.15 版中大约第 587 行的 gauth.py(gdata 客户端库的一部分)中。
传递给 urllib.quote 的“max-results”参数的值是 10000,一个整数,而不是字符串,因此它没有迭代器。
我的快速破解方法是:
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))
您可以使用 pdb 自己追踪问题,如下所示:
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
如果一切正常(比如当你将我的 hack 添加到 gauth.py 时),你会看到这个而不是堆栈跟踪:
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
关于python - python中的gdata分析客户端问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5576210/