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

时间:2016-03-11 22:01:30

标签: python python-requests oauth2

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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