javascript - 如何在我的后端显示我网站的谷歌分析

标签 javascript json google-analytics google-analytics-api

我想在我的管理面板上显示我的谷歌分析。我关注了google analytics api .我创建了服务端授权并下载了 json 数据

{
    "type": "service_account",
    "project_id": "xxxx",
    "private_key_id": "xxxxxxxxxxxxxxxxx",
    "private_key": "-----BEGIN PRIVATE KEY-----xxxxxxxxx-----END PRIVATE KEY-----\n",
     "client_email": "xxxxxxxxxxxxxx",
     "client_id": "xxxxxxxxxxxxxxx",
     "auth_uri": "https://accounts.google.com/o/oauth2/auth",
     "token_uri": "https://accounts.google.com/o/oauth2/token",
     "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
     "client_x509_cert_url":"xxxxxx"
}

在创建上面的 json 并复制该教程中提到的 js 代码之后。

在js代码中要求

   gapi.analytics.auth.authorize({
      'serverAuth': {
      'access_token': '{{ ACCESS_TOKEN_FROM_SERVICE_ACCOUNT }}'
      }
   });

这里的问题是我找不到 ACCESS_TOKEN。我只是用私钥、客户端 ID、私钥 ID 替换并尝试但它显示 401 错误。

可能是它的愚蠢问题。但我不知道如何得到这个。请有人帮助我。

最佳答案

您是否调用了 getAuthResponse?此方法返回您需要使用的访问 token 。

getAuthResponse()

Returns: Object

Gets authentication data returned by the original authorization request. The returned object includes the access token, which can be usually to manually make authenticated requests.

请参阅此处 https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference?hl=en

关于javascript - 如何在我的后端显示我网站的谷歌分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34437451/

相关文章:

javascript - 在解析为 JSON 之前执行 JS 配置(对象)

javascript - 在 YouTube API 中通过 AJAX 访问视频时长

regex - 用于页面URL匹配的Google Analytics(分析)正则表达式

google-analytics - Google Analytics 中没有启用的 View

javascript - 记录执行异步等待功能所花费的时间

javascript - 更改 Flot 条形图上的字体系列

javascript - 单击 JavaScript 中的按钮

javascript - 使用 jquery 替换文本

javascript - 如何访问所有嵌套的关联数组元素?

javascript - 分析 API 包括子页面