1

I have this code:

import requests
import json 

data={"client_id" : "a", "client_secret" : "thisissecret", "grant_type" : "clientcredentials", "scope" : "PublicApi"}

url = 'http://MYURL/connect/token'
response = requests.post(url, json=data, verify=False)
print(response)
print response.reason
print(response.json())

I'm trying to test connection to a new auth service in test environment (which is why verify is FALSE) This should give me the access token and the token type and with them I can POST to the API.

But I always get:

<Response [400]>  Bad Request   {u'error': u'invalid_request'}

I'm not sure what is the problem? Why is it a bad request?

7
  • If possible post the 'MYURL/connect/token' endpoint code Commented Jun 6, 2018 at 7:40
  • @RanaAhmed Hi, the url itself is OK. I tested the same request with PostMan getpostman.com and it worked. I suspect this is something to do with how I built this code... Commented Jun 6, 2018 at 7:43
  • This might work? stackoverflow.com/a/45058650/4909087 Commented Jun 6, 2018 at 7:49
  • Alternatively, you might want to change json=data to json=json.dumps(data) but call import json first. Commented Jun 6, 2018 at 7:50
  • @coldspeed same error still appear Commented Jun 6, 2018 at 7:56

2 Answers 2

2

Looks like you are trying to obtain an OAuth 2.0 access token using the client_credientials grant. This is described in RFC6749

I see 2 problems here:

  • You must post your fields as application/x-www-form-urlencoded instead of json. To do so, use the data parameter of request.post() instead of json
  • The grant_type value must be client_credentials instead of clientcredentials

Which gives:

import requests

data = {"client_id" : "a", "client_secret" : "thisissecret", 
        "grant_type" : "client_credentials", "scope" : "PublicApi"}

url = 'http://MYURL/connect/token'
response = requests.post(url, data=data, verify=False)
if response.ok:
    print(response.json())
Sign up to request clarification or add additional context in comments.

5 Comments

Traceback (most recent call last): File "import.py", line 13, in <module> if response.is_ok: AttributeError: 'Response' object has no attribute 'is_ok'
typo, fixed now
Awesome! it works. How do I access the info inside the response.json?
response.json() returns a standard Python dict (which is the result of parsing the JSON response), so you can read it just like a dict (e.g: to get the access token: at = response.json()['access_token'])
BTW - say respond.ok is not True. What try catch I should use here? What exception will it raise?
0

Perhaps you need to set the content type of the header?

import requests
import json 

data={"client_id" : "a", "client_secret" : "thisissecret", "grant_type" : "clientcredentials", "scope" : "PublicApi"}
headers = {'content-type': 'application/json'}

url = 'http://MYURL/connect/token'
response = requests.post(url, json=data, verify=False, headers=headers)

2 Comments

requests will take care of that for you when you use json={"your":"data"} parameter to requests.post()
Didn't know that. From: docs.python-requests.org/en/master/user/quickstart Using the json parameter in the request will change the Content-Type in the header to application/json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.