ng150716
ng150716

Reputation: 2244

How to Translate CURL to Python Requests

I am currently trying to integrate Stripe Connect and have come across the flowing CURl POST request:

curl https://connect.stripe.com/oauth/token \
   -d client_secret=SECRET_CODE \
   -d code="{AUTHORIZATION_CODE}" \
   -d grant_type=authorization_code

However I am very new to CURL and have been doing some research and trying to use the requests package to do it. This is what my current code looks like:

data = '{"client_secret": "%s", "code": "%s", "grant_type": "authorization_code"}' % (SECRET_KEY, AUTHORIZATION_CODE) 
response = requests.post('https://connect.stripe.com/oauth/token', json=data)

However this always returns a response code 400. I have no idea where I am going wrong and any guidance would be thoroughly appreciated.

Upvotes: 1

Views: 894

Answers (1)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48110

The error is because you are passing your data as string, instead json param of requests.post call expects it to be dict. Your code should be:

import requests

data = {
     "client_secret": SECRET_KEY, 
     "code": AUTHORIZATION_CODE, 
     "grant_type": "authorization_code"
} 

response = requests.post('https://connect.stripe.com/oauth/token', json=data)

Take a look at request library's More complicated POST requests document.

Upvotes: 2

Related Questions