0

I want to get an authorization token using basic authorization. I send a post request using my user name and password but to get the token a body data which is raw text grant_type=client_credentials&scope=Dashboard must contain in the request. but I cannot send the grant_type=client_credentials&scope=Dashboard body data in the post request using python.

    @task(1)
    def login(self):
        self.client.post("/OAuth/Token/", {'Username':'abc', 'Password':'12345'})
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
Sakib Espak
  • 345
  • 2
  • 6
  • 16
  • 1
    what is `self.client.post` in this context? Are you using some kind of OAuth provider? Ifso, which? Also, `grant_type=client_credentials&scope=Dashboard` looks more like parameters instead of body – Edo Akse Apr 22 '19 at 13:02
  • self.client.post is just a post request "grant_type=client_credentials&scope=Dashboard" is a body data which is in text format – Sakib Espak Apr 22 '19 at 13:12
  • @alec_a I used "grant_type=client_credentials&scope=Dashboard" as a body data (text) in postman – Sakib Espak Apr 22 '19 at 13:13
  • The user name and password used for basic authorization – Sakib Espak Apr 22 '19 at 13:14

2 Answers2

1

self.client.post() returns a Response object. You can see the api at https://requests.readthedocs.io/en/latest/api/#requests.Response

To read out something in the response you can try something like

@task(1)
def login(self):
    res = self.client.post("/OAuth/Token/", {'Username':'abc', 'Password':'12345'})
    token = res.json()['token']

This attempts to process the Response body as json and pull out the token field. If this doesn't work please provide details on what you are seeing in the response.

Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
0

Please try this:

In the URL, Append the Grant type and Scope as shown below:

/OAuth/Token?grant_type=client_credentials&scope=Dashboard

It will look like this

self.client.post("/OAuth/Token?grant_type=client_credentials&scope=Dashboard", {'Username':'abc', 'Password':'12345'})

Seema Nair
  • 155
  • 7