0

I'm trying to generate a user authentication token by posting the user name, password in a URL-encoded form, and received a JWT token.

I have tried using payload instead of header and both, but nothing works. I'm using Python 3.7.

import requests

headers = {'username':'username', 'password':'password'}
r = requests.post("https://sso.xxxxxx.com/sso-api/v1/token", headers=headers)
print (r)

I expected the output Response 200 Token generated successfully, but I'm receiving the error Response 404

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
MadLabsPR
  • 1
  • 4
  • First of all you're trying to send username and password as headers. If you want to send it as URL encoded then you should do something like described [here](https://stackoverflow.com/questions/26615756/python-requests-module-sends-json-string-instead-of-x-www-form-urlencoded-param). Also you need to know beforehand how your API sends token to you and depending on that you can recieve your token in json or via header. – devaerial Jul 22 '19 at 12:56
  • Can you post what response you'r recieving (headers, body etc.)? – devaerial Jul 24 '19 at 12:28
  • I was able to run the code with the suggestion that you mention. I decoded the JWT and receive the following headers - 'access_token': 'xxxx..', 'id_token': 'xxxxx...', 'token_type': 'Bearer'. Now, I need to use the token to download a report using requests.get, but don't have any idea on how to use the token. – MadLabsPR Jul 25 '19 at 14:32
  • Most APIs expect token to be passed in headers. The general rule is to send token in `Authorization` header like this: `Authorization: Bearer {your token here}` – devaerial Jul 25 '19 at 14:39

1 Answers1

0

I was able to to do it the the tips and help from xbound. Instead of header, I had to pass it as data. Then I confirmed that the response was ok and extracted the token.

import requests
payload = {'username':'*******@*********.com', 'password':'**********', 'grant_type':'password', 'scope':'openid'}
r = requests.post("https://***.******.com/***/**/token", data = payload)

##Confirm that reponse is OK!
r.status_code
print(r.status_code == requests.codes.ok)

##Decode json reponse - token
data = r.json()

##Select token
my_token = data['id_token']
MadLabsPR
  • 1
  • 4