Python中gdata分析客户端的问题

0 投票
2 回答
706 浏览
提问于 2025-04-16 15:15

我成功地用Google的gdata python库获取了Google Analytics的OAuth访问令牌。

不过,我用这个令牌去访问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)

我得到了以下的错误信息:

追踪信息(最近的调用在最前面):
文件 "/Library/Python/2.6/site-packages/django/core/servers/basehttp.py", 第280行,在运行时 self.result = application(self.environ, self.start_response)

文件 "/Library/Python/2.6/site-packages/django/core/servers/basehttp.py", 第674行,在 调用 return self.application(environ, start_response)

文件 "/Library/Python/2.6/site-packages/django/core/handlers/wsgi.py", 第248行,在 调用 response = self.get_response(request)

文件 "/Library/Python/2.6/site-packages/django/core/handlers/base.py", 第141行,在获取响应时 return self.handle_uncaught_exception(request, resolver, sys.exc_info())

文件 "/Library/Python/2.6/site-packages/django/core/handlers/base.py", 第100行,在获取响应时 response = callback(request, *callback_args, **callback_kwargs)

文件 "/Library/Python/2.6/site-packages/django/contrib/auth/decorators.py", 第25行,在 _wrapped_view return view_func(request, *args, **kwargs)

文件 "/Users/***/***/**/***/**/googleAnalyticsOauth.py", 第122行,在 googleAnalyticsTest data = client.GetDataFeed(dataQuery)

文件 "build/bdist.macosx-10.6-universal/egg/gdata/analytics/client.py", 第77行,在 get_data_feed **kwargs)

文件 "build/bdist.macosx-10.6-universal/egg/gdata/client.py", 第635行,在 get_feed **kwargs)

文件 "build/bdist.macosx-10.6-universal/egg/gdata/client.py", 第265行,在请求 uri=uri, auth_token=auth_token, http_request=http_request, **kwargs)

文件 "build/bdist.macosx-10.6-universal/egg/atom/client.py", 第110行,在请求 self.auth_token.modify_request(http_request)

文件 "build/bdist.macosx-10.6-universal/egg/gdata/gauth.py", 第980行,在 modify_request token_secret=self.token_secret, verifier=self.verifier)

文件 "build/bdist.macosx-10.6-universal/egg/gdata/gauth.py", 第604行,在生成_hmac签名 next, token, verifier=verifier)

文件 "build/bdist.macosx-10.6-universal/egg/gdata/gauth.py", 第565行,在构建_oauth基础字符串 urllib.quote(params[key], safe='~')))

文件 "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", 第1216行,在 quote res = map(safe_map.getitem, s)

类型错误:map()的第二个参数必须支持迭代

有没有人知道可能出什么问题了?

谢谢!

2 个回答

0

供参考,你需要做以下操作:

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

来源

2

问题出在gauth.py这个文件里,它是gdata客户端库的一部分,大约在版本2.0.15的第587行。

这里有一个参数叫“max-results”,它传给了urllib.quote,但这个参数的值是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

如果一切正常(比如你把我上面的解决方法加到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

撰写回答