2016-01-22 92 views
0

我需要執行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

回答

0

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 
相關問題