使用Python访问Google API时“无效的issuer:null”

时间:2016-07-04 23:50:57

标签: python google-api jwt chrome-web-store pyjwt

我正在尝试通过API上传Chrome扩展程序。第一步是获取oauth-token但我收到此错误:

{
 "error": "invalid_grant",
 "error_description": "Invalid issuer: null"
}

这是我的剧本:

# pip install pyjwt

import jwt
import time
import base64
import requests
import json
from copy import copy

JWT_INPUT = '{header}.{claim_set}'
HEADER = {"alg":"RS256", "typ":"JWT"}
ALGORITHM = 'RS256'  # required by google
EMAIL = 'deploy@<Service account ID>.iam.gserviceaccount.com'
# TODO: which one is correct? any?
SCOPE = 'https://www.googleapis.com/auth/chromewebstore https://www.googleapis.com/auth/chromewebstore.admin'
HOST = 'https://www.googleapis.com/oauth2/v4/token'
HEADERS = {
    'Content-Type': 'application/x-www-form-urlencoded',
}

CLAIM_SET = {
    "iss": EMAIL,
    "scope": SCOPE,
    "aud": "https://www.googleapis.com/oauth2/v4/token",
    "exp": None,  # now +1h since epoch
    "iat": None  # now since epoch
}

claim_set = copy(CLAIM_SET)
claim_set['iat'] = int(time.time())
claim_set['exp'] = claim_set['iat'] + 60 * 60

data = {  
    'header': base64.b64encode(json.dumps(HEADER)),
    'claim_set': base64.b64encode(json.dumps(claim_set)),
}

jwt_string = JWT_INPUT.format(**data)
jwt_token = jwt.encode(data, open('private.key').read(), algorithm='RS256')
print jwt_token 
print

params = {
    'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion': jwt_token,
}
response = requests.post(HOST, data=params, headers=HEADERS)
print response.content

0 个答案:

没有答案