Calling Okta APIs form python - invalid header

Hi,

I’m trying to call the Create User Without Credentials API from within a Python app. This works fine when using Postman or Insomnia with the API token assigned to the app. However, in Python 3.6.5 I get the following error:

Invalid header name ‘Authorization:’

I am using the requests module. Here is my code (API key is retrieved from env variable and user data is passed into the enclosing function as parameters). I have removed error trapping, comments etc:

api_token = settings.OKTA_API_TOKEN
endpoint = settings.OKTA_NEW_USER_API_URL

   headers = {
       "Accept": "application/json",
       "Content-Type": "application/json",
       "Authorization:": f"SSWS {api_token}",
   } 

   payload = {
       "profile": {
            "firstName": first_name,
            "lastName": last_name,
            "email": email,
            "login": email,
            "mobilePhone": mobile_phone
    }
}

 # FAILS HERE => ValueError: Invalid header name b'Authorization:'
response = requests.post(   
    url=endpoint,
    headers=headers,
    json=payload,
)

If I try to move the Auth details into the requests.post’s auth parameter, no matter how I set the tuple, I cannot get it to accept the token - I always get a 403 with an invalid session error in the errorSummary return value)

Could somebody advise as to what I am doing wrong above? Many thanks.

Sorry, couldn’t get the indenting to line up correctly using the forum’s code formatting options - but to be clear, the indenting is not the issue - it’s just when I paste it here that it fails to format correctly

Oh dear - I have a colon in the header name.

Well that’s embarrassing :smiley:

3 Likes

I didn’t see that at first either. Glad you solved it! :wink:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.