Google API服务帐户访问令牌过期

时间:2017-12-07 14:42:46

标签: python google-api google-analytics-api access-token service-accounts

我想检索realtime user data from Google Analytics and this article by Google概述如何(并按预期工作)。

 gapi.client.analytics.data.realtime.get({
                    "ids": "ga:21660971",
                    "metrics": "rt:activeUsers"
                })

但是我关注the server-side authentication,因为我希望无权访问的用户始终可以看到它。但是一夜之间,这个访问令牌变得无效。我可以做些什么或做哪些工作来满足需要不断进行人工干预来一直刷新令牌的工作!

Python脚本(如Google文章中所述)。

# service-account.py

import time

from oauth2client.service_account import ServiceAccountCredentials
# 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 = 'MY-JSON-FILE.json'

# Defines a method to get an access token from the ServiceAccount object.
def get_access_token():
  return ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILEPATH, SCOPE).get_access_token().access_token

print(get_access_token())

JS Authentification

    /**
     * Authorize the user with an access token obtained server side.
     */
    gapi.analytics.auth.authorize({
        'serverAuth': {
            'access_token': 'TOKEN-FROM-PY-SCRIPT-ABOVE'
        }
    });

这是控制台中给出的错误,它只在60分钟左右后执行此操作。

  

{domain:" global",reason:" authError",message:" Invalid   凭证",locationType:"标题",位置:"授权"}

     

数组(0)消息

     

"凭证无效"

0 个答案:

没有答案