Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Oauth Access Token in Python

I am trying to generate a oauth access token for experian's sandbox API (they give information on credit information). Their tutorial Says to run this (fake data) to get an access token:

curl -X POST
-d '{ "username":"[email protected]", "password":"YOURPASSWORD"}'
-H "Client_id: 3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9"
-H "Client_secret: ipu3WQDqTEjqZDXW"
-H "Content-Type: application/json"
"https://sandbox-us-api.experian.com/oauth2/v1/token"

How would I run this in python? I tried this among a lot of other things:

data = { "username" : "[email protected]", "password":"YOURPASSWORD"}

headers = {"Client_id": "3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9", "Client_secret": "ipu3WQDqTEjqZDXW", "Content-Type": "application/json"}

response = requests.post("https://sandbox-us-
api.experian.com/oauth2/v1/token", data=data, headers=headers)

Any help would be greatly appreciated

like image 299
user123 Avatar asked Mar 07 '23 13:03

user123


1 Answers

Almost there, just need to parse the response:

import json

data = { "username" : "[email protected]", "password":"YOURPASSWORD"}

headers = {"Client_id": "3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9", "Client_secret": "ipu3WQDqTEjqZDXW", "Content-Type": "application/json"}

response = requests.post("https://sandbox-us-api.experian.com/oauth2/v1/token", data=data, headers=headers)
if response.status_code in [200]:
    tok_dict = json.loads(response.text)
    print(tok_dict)
    issued_at = tok_dict["issued_at"]
    expires_in = tok_dict["expires_in"]
    token_type = tok_dict["token_type"]
    access_token = tok_dict["access_token"]
else:
    print(response.text)
like image 132
alexisdevarennes Avatar answered Mar 27 '23 03:03

alexisdevarennes