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

时间:2021-02-04 15:13:37

I want to show my google analytics on my admin panel. I have followed the google analytics api . I created service side authorization and downloaded the json data

我想在我的管理面板上显示我的谷歌分析。我使用了谷歌分析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"
}

After creating above json and copied js code mentioned in that tutorial.

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

In the js code its asked for the

在js代码中,它要求

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

Here the problem is i can't find the ACCESS_TOKEN. I just replaced with private key, client id, private key id and tried but it shows 401 error.

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

May be its silly problem. but i don't know how to get this. Please someone help me.

也许这是个愚蠢的问题。但是我不知道怎么得到这个。请一个人帮助我。

2 个解决方案

#1


1  

Did you call getAuthResponse? This method returns the access token which you need to use.

你叫getAuthResponse吗?此方法返回您需要使用的访问令牌。

getAuthResponse()

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.

获取原始授权请求返回的身份验证数据。返回的对象包括访问令牌,通常可以手动发出经过身份验证的请求。

See here https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference?hl=en

看到https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference?hl=en

#2


0  

Unfortunatly you skipped step 3 from the linked documentation.

不幸的是,您从链接文档中跳过了第3步。

Step 3: Use the JSON key data to request an access token

The access token is obtained from running the python code:

访问令牌是通过运行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

The example you have linked to is opensource and this method is called on the server side and the access token is returned through a web application template variable.

您所链接的示例是opensource,该方法在服务器端调用,访问令牌通过web应用程序模板变量返回。

#1


1  

Did you call getAuthResponse? This method returns the access token which you need to use.

你叫getAuthResponse吗?此方法返回您需要使用的访问令牌。

getAuthResponse()

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.

获取原始授权请求返回的身份验证数据。返回的对象包括访问令牌,通常可以手动发出经过身份验证的请求。

See here https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference?hl=en

看到https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference?hl=en

#2


0  

Unfortunatly you skipped step 3 from the linked documentation.

不幸的是,您从链接文档中跳过了第3步。

Step 3: Use the JSON key data to request an access token

The access token is obtained from running the python code:

访问令牌是通过运行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

The example you have linked to is opensource and this method is called on the server side and the access token is returned through a web application template variable.

您所链接的示例是opensource,该方法在服务器端调用,访问令牌通过web应用程序模板变量返回。