Michael Queue
Michael Queue

Reputation: 1400

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

I am using the requests library to make a POST request in order to obtain an access token. My request works properly, but, I'm not sure how to extract it and then use it in a GET request.

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

Upvotes: 1

Views: 4131

Answers (2)

tuned
tuned

Reputation: 1125

@michael-queue the response from the request to a JSON endpoint is a JSON encoded string. To load it into a dictionary and access inner properties it's needed to json.loads(json_string) into Python. For the opposite operation, to dump a dictionary into a JSON string is json.dumps(dictionary) instead.

Upvotes: 1

jbndlr
jbndlr

Reputation: 5210

If it returns a dict, why not just access its contents as usual?

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

Upvotes: 2

Related Questions