Connect to Okta API's by using Python

Below is the Python script which i am using to connect to okta get users details.

import requests
import json
api_token = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’

result = requests.get(‘https://{domain}.oktapreview.com/api/v1/users/me’)
headers = {‘Content-Type’: ‘application/json’,‘Accept’:‘application/json’,
‘Authorization’:‘SSWS {api_token)’}
print(result)

Issue :response is always 403
Thanks in advance please help me in this.

Hi @sriajay

403 errors occur usually when the API token is not correctly sent to the endpoint. F

rom what I can see, the api_token is not correctly added under ‘Authorization’:‘SSWS {api_token)’}, more specifically there is a curly brace and a paranthesis for api_token.

Can you please correct this and try again?

Still the response is same 403

result = requests.get(‘https://{domain}.oktapreview.com/api/v1/users/me’)
headers = {‘Content-Type’: ‘application/json’,‘Accept’:‘application/json’,
‘Authorization’:‘SSWS {api_token}’}
print(result)

Hi @sriajay

If you are adding the api_token directly as a string instead of variable under the Authorization server, does the request return the user’s details successfully?

No it is still not returning and value even hard coding the api token value.

result = requests.get(‘https://{domain}.oktapreview.com/api/v1/users’)
headers = {‘Content-Type’: ‘application/json’,‘Accept’:‘application/json’,
‘Authorization’: ‘SSWS {REMOVED}’}
print(result)

still the result is same 403

Hi @sriajay

I have removed the API token due to possible security issues that might occur, as this is a public forum.

Can you please try to remove the curly brackets { } from the authorization header and try again?

Hi @dragos,
Thanks for prompt reply but still no luck.

Hi @sriajay

Here is a Python example generated from Postman to retrieve the details about the current user


url = "https://org.okta.com/api/v1/users/me"

payload = ""
headers = {
    'Accept': "application/json",
    'Content-Type': "application/json",
    'Authorization': "SSWS 00b[...]Fi"
    }

response = requests.request("GET", url, data=payload, headers=headers)

print(response.text)

Can you please test it out, modifying org.okta.com with your current Okta preview org and 00b[...]Fi with your API token?

import requests
import json

api_token=‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
url = “https://dev-338993-admin.oktapreview.com/api/v1/users/

payload = “”
headers = {
‘Accept’: “application/json”,
‘Content-Type’: “application/json”,
‘Authorization’: “SSWS 00b[api_token}Fi”

‘Authorization’: ‘SSWS {}’.format(‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’)

}

response = requests.request(“GET”, url, data=payload, headers=headers)

print(response.text)

error code:{“errorCode”:“E0000011”,“errorSummary”:“Invalid token provided”,“errorLink”:“E0000011”,“errorId”:“oaea_HXglVbSFKLtbGQVTmffw”,“errorCauses”:[]}

NOTE :The same API token is working in Postman

Hi @dragos,
Can you please help me on this.