Jaroslav Pinkas
Jaroslav Pinkas

Reputation: 11

Python API request to internal API with OKTA Authentication

I used to selenium for downloading special reports from webpage where I have to login. Webpage has integrated OKTA Authentication plugin . I find out that there would be better and more effective use internal API requests. So I tried find how to use request python library with creating session, but I am unsuccessful. I tried this code, but it ends with 400 error.

payload = {"password":"password","username":"username","options":{"warnBeforePasswordExpired": True,"multiOptionalFactorEnroll": True}}

with requests.Session() as s:
    p = s.post('https://sso.johndeere.com/api/v1/authn', data=payload)
  
    r = s.get("requested_url")
    
    print(p)

I am unable get throw auth. Has anybody experience with breaking OKTA auth plugin using requests library?

Thanks Best Regards

Upvotes: 0

Views: 2507

Answers (1)

Just some sailboat
Just some sailboat

Reputation: 330

Firstly, an HTTP error code of 400 error means one or more settings is wrong at the client side. You can learn more about it here.

You seem to be missing out important headers configuration. You need to set the content-type header correctly otherwise the destination server won't be able to process your data.

Also, as a bonus point. You need to format your payload into a valid JSON string before sending out the request too.

import requests
import json

# Setup proper headers
headers = {
    "accept": "application/json, text/plain, */*",
    "content-type": "application/json; charset=UTF-8"
}

# Your body data here
payload = {"password":"password","username":"username","options":{"warnBeforePasswordExpired": True,"multiOptionalFactorEnroll": True}}
payload_json = json.dumps(payload) # Format it into a valid JSON str


with requests.Session() as s:
    p = s.post('https://sso.johndeere.com/api/v1/authn', headers=headers, data=payload_json)

    r = s.get("requested_url")
    
    print(p.content)

Upvotes: 0

Related Questions