Python从密钥分析嵌入API中提取访问令牌

时间:2015-07-29 11:17:57

标签: python google-analytics

我之前并没有真正使用过Python,但它是Google为您提供的唯一示例...

我已经测试了我的JS / HTML代码,使用来自OAuth 2.0 playground的临时令牌向我们网站上的客户显示Analytics仪表板。一切正常,但当然令牌仅持续一个小时。

他们为您提取密钥的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

我已将json密钥的文件路径更改为文件的位置,我已经为Analytics API安装了所需的python客户端,并检查了它是否已安装。

当我在浏览器中访问python脚本时,我收到内部服务器错误,这是可以理解的但是如何将密钥放入我的HTML / PHP页面?或者是否有一个替代PHP脚本而不是提取密钥?

他们为前端(密钥应该去的地方)提供的代码是:

gapi.analytics.ready(function() {

  /**
   * Authorize the user with an access token obtained server side.
   */
  gapi.analytics.auth.authorize({
    'serverAuth': {
      'access_token': ' [ ACCESS TOKEN HERE] '
    }
});

0 个答案:

没有答案