
AviS.97166 (Customer) asked a question.
Can we connect to okta APIS by using Python??
I am getting error when trying to connect to Get Users API with the below code:
import requests
import json
api_token = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
headers = {‘Content-Type’: ‘application/json’,‘Accept’:‘application/json’,
‘Authorization’:‘SSWS {api_token)’}
result = requests.get(‘https://{domain}.oktapreview.com/api/v1/users’)
Please help me with the code if you know how to connect to Okta APIS by using Python???

Hi,
Have you tried to add the headers in the request? e.g:
result = request.get('‘https://{domain}.oktapreview.com/api/v1/users’, headers=headers)
Also in the headers it says Authorization’:‘SSWS {api_token)' (you are starting with a "{" and ending with a ")". For testing, replace ")" with a "}" or hardcode the token inside the string.
e.g: 'Authorization': 'SSWS XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
I hope it helps, let me know if you need additional assistance!
/Alexander
Hii Alexander Ling
I have tried hardconding the API Token and sending request with headers.
Still the error is:
HTTPSConnectionPool(host='oktadomiain.okta.com', port=443): Max retries exceeded with url: /api/v1/users (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f8dc27bd070>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
Please help me resolving it if you can.
Thanks for your time and help.
Hi,
what url are you using? Seems like oktadomiain.okta.com is not correct. What is your domain called? Because the error is telling you that it cannot find the specified endpoint.
Hi Alexander
I have replaced it here but i am using the correct url of my developer tenant.
I am using https://dev-number.okta.com/api/v1/users
there
So this is what you are trying?
result = request.get("https://dev-number.okta.com/api/v1/users", headers=headers)
Yes with the headers declared and defined above it.
Hi,
I am not sure what you are doing, this works without any problems for me. Just replace ADD_TOKEN_HERE with your token. Do not add or remove any other characters like ' " ) ( just add the token inside the string.
import requests
import json
headers = {
"Content-Type": "application/json",
"Accept":"application/json",
"Authorization":"SSWS ADD_TOKEN_HERE"
}
result = requests.get("https://dev-number.okta.com/api/v1/users", headers=headers)
print(result)
Hi Alexander Ling
I was able to trace the issue and solve it. The code was absolutely fine but as I was running it from Online Compiler it gave a connection error as I called the API from Frontend. When called after installing python on local machine works absolutely fine,