如何在后台显示我的网站的谷歌分析
How to show google analytics of my website in my backend
我想在我的管理面板上显示我的谷歌分析。我使用了谷歌分析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
?此方法返回您需要使用的访问令牌。
getAuthResponse()
返回:对象
获取原始授权返回的身份验证数据要求返回的对象包括访问令牌,该令牌可以是通常手动发出经过身份验证的请求。
请参阅此处https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference?hl=en
不幸的是,您跳过了链接文档中的步骤3。
步骤3:使用JSON密钥数据请求访问令牌
访问令牌是通过运行python代码获得的:
# service-account.py
import json
from oauth2client.client import SignedJwtAssertionCredentials
# The scope for the OAuth2 request.
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
# The location of the key file with the key data.
KEY_FILEPATH = 'path/to/json-key.json'
# Load the key file's private data.
with open(KEY_FILEPATH) as key_file:
_key_data = json.load(key_file)
# Construct a credentials objects from the key data and OAuth2 scope.
_credentials = SignedJwtAssertionCredentials(
_key_data['client_email'], _key_data['private_key'], SCOPE)
# Defines a method to get an access token from the credentials object.
# The access token is automatically refreshed if it has expired.
def get_access_token():
return _credentials.get_access_token().access_token
您链接到的示例是开源的,此方法在服务器端调用,访问令牌通过web应用程序模板变量返回。
相关文章:
- 从应用程序脚本检查谷歌网站访问权限
- 使用谷歌网站翻译器自动翻译网页
- 如何在谷歌网站中使用javascript获取当前页面的网址
- JavaScript和CSS样式不适用于我的“;谷歌网站”;页
- 如何在谷歌网站中显示与iframe中的特定人员共享的谷歌云端硬盘html链接
- 谷歌网站优化器脚本中的混淆脚本标记
- 检测谷歌网站翻译器的语言变化
- 以编程方式声明基于索引数组的变量(使用谷歌网站列表页面)
- 如何在谷歌网站中包含带有HTML的javascript
- 如何按程序更改谷歌网站翻译器中的语言
- 谷歌网站自动登录
- 如何获得谷歌网站管理员api(Node.js)的访问令牌
- 防止谷歌分析(ga)和谷歌网站优化器(GWO)的ga.js重复
- 谷歌网站翻译打破搜索
- 谷歌网站HTML框是't识别CSS或Javascript
- 谷歌网站搜索捕获搜索提交和触发功能
- 从谷歌网站获取信息(内部HTML-可以这么认为)
- 谷歌网站中HTML框的大小
- 检测谷歌网站上的黑莓浏览器
- 谷歌网站HTML Iframe(不是小工具)