1

我正在使用 requests 库发出 POST 请求以获取访问令牌。我的请求工作正常,但是,我不确定如何提取它然后在 GET 请求中使用它。

url = 'https://login.insideview.com/Auth/login/v1/token'
payload = {'clientId' : '****', 'clientSecret' : '****','grantType':'cred'}
headers = { 'Accept' : 'application/json'}
r = requests.post(url, headers=headers, params=payload)

solution:  
data = json.loads(r.text)
data['accessTokenDetails']['accessToken']

回报:

{"accessTokenDetails":{"accessToken":"the_access_token","tokenType":"bearer","expirationTime":"Fri, Mar 25, 2016 09:59:53 PM GMT","userInfo":{"userId":null,"firstName":null,"lastName":null,"userName":null,"companyName":null,"accountId":null,"role":null}}}
4

2 回答 2

1

如果它返回 a dict,为什么不像往常一样访问它的内容呢?

token = r['accessTokenDetails']['accessToken']
于 2016-03-11T22:17:43.493 回答
1

@michael-queue 从请求到 JSON 端点的响应是 JSON 编码的字符串。json.loads(json_string)要将其加载到字典中并访问Python 中需要的内部属性。对于相反的操作,将字典转储为 JSON 字符串json.dumps(dictionary)

于 2016-03-11T22:39:49.580 回答