【问题标题】:Google Analytics Core Reporting API Python library谷歌分析核心报告 API Python 库
【发布时间】:2012-03-14 23:52:08
【问题描述】:

我开始研究 Google Analytics 核心报告 API,它现在是第 3 版。

根据文档,我可以使用链接http://code.google.com/apis/analytics/docs/gdata/v3/gdataLibraries.html 中列出的客户端库之一。

我正在使用 python,所以我正在寻找一个在 python 中使用核心报告 API 的示例,但我找不到使用这个库的示例。 http://code.google.com/p/google-api-python-client/wiki/SampleApps 的示例均未包含​​核心报告 API 的示例。

另一个选项似乎是使用http://code.google.com/p/gdata-python-client/ 的库,但我不确定该库是否使用最新版本的核心报告 API (v3.0)。

我正在寻找符合http://code.google.com/apis/analytics/docs/gdata/v3/reference.html 的python 库(带有文档/示例)

谢谢

【问题讨论】:

    标签: python gdata-api google-analytics-api


    【解决方案1】:

    我没有找到任何示例或好的文档,但我能够将通用 oauth2 身份验证与 JAVA 示例和 python 库源代码混合以找到答案。所以,就这样吧:

    身份验证:

    from oauth2client.file import Storage
    from oauth2client.client import AccessTokenRefreshError
    from oauth2client.client import OAuth2WebServerFlow
    from oauth2client.tools import run
    import httplib2
    
    FLOW = OAuth2WebServerFlow(
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET,
        scope='https://www.googleapis.com/auth/analytics.readonly')
    storage = Storage('file_name.dat')
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = run(FLOW, storage)
    http = credentials.authorize(httplib2.Http())
    

    连接到核心报告 API(我不确定动词“连接”是否足够)

    from apiclient.discovery import build
    service = build('analytics', 'v3', http=http)
    

    进行查询:

    query = service.data().ga().get(ids='ga:%d' % PROFILE_ID, start_date=START_DATE, end_date=END_DATE,metrics='ga:pageviews')
    results = query.execute()
    

    创建查询时传递给 get 方法的完整参数列表可以在 http://api-python-client-doc.appspot.com/analytics/v3/data/ga 找到。

    结果完全按照http://code.google.com/apis/analytics/docs/gdata/v3/reference.html#data_response 中的描述以python dict 形式出现

    【讨论】:

    • 此代码实际上需要打开浏览器并完成身份验证流程。不知道如何在服务器上运行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多