【问题标题】:Authenticating with the new Google Analytics Core Reporting API and OAuth 2.0使用新的 Google Analytics Core Reporting API 和 OAuth 2.0 进行身份验证
【发布时间】:2015-03-21 04:13:56
【问题描述】:

Google Analytics 的新“核心报告 API”(3.0 版)“建议使用 OAuth 2.0 对请求进行授权”(citation)。但是,它的文档非常不清楚如何做到这一点。 (上面写着“当您创建应用程序时,您向 Google 注册了它”(citation),但是 shell 脚本算作“应用程序”吗??如果是这样,我应该在 the "APIs Console" 注册 bash 脚本,这没有提供任何指导。)使用 Analytics 的 2.3 版,我运行了一个 bash 脚本:

#!/bin/bash
# generates an XML file

googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s \
  -d Email=foo \
  -d Passwd=bar \
  -d accountType=GOOGLE \
  -d source=curl-dataFeed-v2 \
  -d service=analytics \
  | awk /Auth=.*/)"

# ...

feedUri="https://www.google.com/analytics/feeds/data\
?ids=$table\
&start-date=$SD\
&end-date=$ED\
&dimensions=baz\
&metrics=xyzzy\
&prettyprint=true"

# ...

curl $feedUri --silent \
  --header "Authorization: GoogleLogin $googleAuth" \
  --header "GData-Version: 2" \
  | awk # ...

我将如何为新的 Analytics 做这样的事情 - 一个抓取我需要的任何登录令牌并将其发送回的脚本?

(顺便说一句,是的,我意识到结果将是 JSON,而不是 XML。)

【问题讨论】:

标签: api bash google-analytics oauth-2.0


【解决方案1】:

这是一个示例脚本

googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s \
 -d Email=$USER_EMAIL \
 -d Passwd=$USER_PASS \
 -d accountType=GOOGLE \
 -d source=curl-accountFeed-v1 \
 -d service=analytics \
 | grep "Auth=" | cut -d"=" -f2)"

feedUri="https://www.googleapis.com/analytics/v3/data/ga\
?start-date=$START_DATE\
&end-date=$END_DATE\
&ids=ga:$PROFILE_ID\
&dimensions=ga:userType\
&metrics=ga:users\
&max-results=50\
&prettyprint=false"

curl $feedUri -s --header "Authorization: GoogleLogin auth=$googleAuth"

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2021-06-08
    • 2014-05-03
    • 2018-07-21
    • 1970-01-01
    • 2018-10-03
    相关资源
    最近更新 更多