如何使用服务帐户从Google Analytics获取访问令牌?

问题描述:

我需要执行POST请求以使用服务帐户从Google Analytics获取访问令牌。如何使用服务帐户从Google Analytics获取访问令牌?

我需要绕过浏览器中手动授权,所以我用了我所所有的细节,PRIVATE_KEY,CLIENT_ID等

https://www.googleapis.com/oauth2/v4/token&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion= {专用密钥}

如果我做了一个服务帐户以上,我收不到。

任何人都可以帮忙吗?

干杯

+0

获取接入人工不易这里是一些文档。 https://developers.google.com/identity/protocols/OAuth2ServiceAccount我建议您抓住Google客户端库并挑选其代码。 – DaImTo

Server Side Authorization嵌入API演示了这样的一个例子。该示例使用Python的Google API客户端库,但是,作为@DalmTo在他们的评论中提到,您可以查看它在底层进行的操作以复制请求。

这里好像什么获得访问令牌代码如下:使用服务帐户

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