【问题标题】:issue with gdata analytics client in pythonpython中的gdata分析客户端问题
【发布时间】:2011-04-07 05:01:08
【问题描述】:

我能够使用 Google 的 gdata python 库为 Google Analytics 成功检索 OAuth 访问令牌。

但是,我尝试使用令牌访问 Google Analytics(分析)数据的尝试失败了。下面是相关代码sn-p:

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 = 应用程序(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 行,在 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 return view_func(request, *args, **kwargs)

文件 "/Users/***/***/ **/***/* */googleAnalyticsOauth.py", 第 122 行,在 googleAnalyticsTest 中 数据 = 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 行,在 generate_hmac_signature 下一个,令牌,验证者=验证者)

文件 “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)

TypeError: map() 的参数 2 必须 支持迭代

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

谢谢!

【问题讨论】:

    标签: python google-analytics gdata


    【解决方案1】:

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

    传递给 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
    

    【讨论】:

      【解决方案2】:

      FWIW,必须做到以下几点:

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

      Via

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-22
        • 2023-01-18
        • 1970-01-01
        • 2012-08-08
        • 1970-01-01
        • 1970-01-01
        • 2016-06-22
        相关资源
        最近更新 更多