1

I'm trying to make a POST request to https://accounts.spotify.com/api/token via python and the request, library but can't get it to work. I'm able to execute the request via curl command:

note - params enclosed in * * are correct and work in the curl request

 curl -H "Authorization: Basic *base 64 encoded client ID and secret*"
 -d grant_type=authorization_code -d code=*auth code* -d 
 redirect_uri=https%3A%2F%2Fopen.spotify.com%2F 
 https://accounts.spotify.com/api/token 

and the request works just fine, however when I try to make what I think is the exact same request in python, I always get the same bad request error

 headers = {
 "Authorization": "Basic *base64 encoded client ID and secret*"
 }
 params = {
 "grant_type": "authorization_code",
 "code": code,
 "redirect_uri": "https://open.spotify.com/"
 }
 response = requests.post(
 url,
 params=params,
 headers=headers
 )

If you can help me figure out how the two requests differ and why the python one never seems to work that would be amazing.

see section 2. of https://developer.spotify.com/documentation/general/guides/authorization-guide/ for the params

asked May 5, 2020 at 6:08
1
  • Could you include the actual response content that you get when you run into a BAD_REQUEST via the Python post code? Commented May 5, 2020 at 6:12

2 Answers 2

1

You use -d flag in your curl request which stands for data.

So you should pass your params as data also in your Python POST request:

headers = {
 "Authorization": "Basic *base64 encoded client ID and secret*"
 }
 params = {
 "grant_type": "authorization_code",
 "code": code,
 "redirect_uri": "https://open.spotify.com/"
 }
 response = requests.post(
 url,
 data=params,
 headers=headers
 )
answered May 5, 2020 at 6:18
Sign up to request clarification or add additional context in comments.

Comments

0

seems like you put payload under wrong argument, try to change params into json or data (depends on what type of requests that API accept):

response = requests.post(
 url,
 json=params,
 headers=headers
)
answered May 5, 2020 at 6:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.