python 中的 gdata 分析客户端问题

发布于 2024-10-30 18:15:40 字数 3149 浏览 5 评论 0原文

我能够使用 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.结果 = 应用程序(self.environ, self.start_response)

文件 “/Library/Python/2.6/site-packages/django/core/servers/basehttp.py”, 第 674 行,在通话中 返回 self.application(environ, start_response)

文件 “/Library/Python/2.6/site-packages/django/core/handlers/wsgi.py”, 第 248 行,在通话中 响应 = self.get_response(请求)

文件 “/Library/Python/2.6/site-packages/django/core/handlers/base.py”, 第 141 行,在 get_response 中 返回 self.handle_uncaught_exception(请求, 解析器,sys.exc_info())

文件 “/Library/Python/2.6/site-packages/django/core/handlers/base.py”, 第 100 行,在 get_response 中 响应 = 回调(请求,*callback_args,**callback_kwargs)

文件 “/Library/Python/2.6/site-packages/django/contrib/auth/decorators.py”, 第 25 行,在 _wrapped_view 中 返回 view_func(请求, *args, **kwargs)

文件 "/Users/***/***< /strong>/**/***/**/googleAnalyticsOauth.py", 第 122 行,在 googleAnalyticsTest 中 数据 = client.GetDataFeed(dataQuery)

文件 “build/bdist.macosx-10.6-universal/egg/gdata/analytics/client.py”, 第 77 行,在 get_data_feed 中 **夸格斯)

文件 “build/bdist.macosx-10.6-universal/egg/gdata/client.py”, 第 635 行,在 get_feed 中 **夸格斯)

文件 “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”, generate_hmac_signature 中的第 604 行 接下来,令牌,验证者=验证者)

文件 “build/bdist.macosx-10.6-universal/egg/gdata/gauth.py”, 第 565 行,在 build_oauth_base_string 中 urllib.quote(params[key], safe='~')))

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

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

任何人都知道可能出现什么问题吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2024-11-06 18:15:40

问题出在 2.0.15 版本中的 gauth.py(gdata 客户端库的一部分)中的第 587 行左右。

传递给 urllib.quote 的“max-results”参数的值为 10000,一个整数,而不是字符串,因此它没有迭代器。

我的快速 hacky 修复是:

  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

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
晌融 2024-11-06 18:15:40

FWIW,必须执行以下操作:

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

通过

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文