从 POST 请求中检索访问令牌并在 GET 请求中使用

Retrieve the access token from POST request and use in GET request

我正在使用请求库发出 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']

Returns:

{"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}}}

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

token = r['accessTokenDetails']['accessToken']

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