I am trying to make a request to the Stripe API (which is a payment API) to get a json response which the API gives. I tried the following code I found in a Python course, but as the API needs to authenticate to get the response, I don't know how can I add that data in the request.
Making it in a curl request will be like this:
curl https://api.stripe.com/v1/checkout/sessions -u pk_test_51LoUz3BgfM0E8ZiCV8UO79gw8zw7fhSgHUEAVj4wS7igs5D4kKiNsxXGeKQEUhorImNUiCxCNNtidwNkhFPUHP4i0060lsvsbw: -d success_url="http://127.0.0.1:5500/pages/success.html" -d cancel_url="http://127.0.0.1:5500/pages/nosuccess.html" -d "line_items[0][price]"=price_1LvNRkBgfM0E8ZiCTSiaNvNL -d "line_items[0][quantity]"=1 -d mode=subscription -d client_reference_id="123" -d customer_email="[email protected]" -d client_reference_id="tokenized" -d phone_number_collection["enabled"]=true
This will create a new checkout session in your stripe account and the response is a json with the info of the checkout session.
The authorisation I use in the curl request is the '-u' value of the publishable_key followed by ':' which means no password is required
But when I try to make this in Python I get the following error:
Traceback (most recent call last):
File "/home/pau/Desktop/bsnbl/Backend/borrar.py", line 17, in <module>
respuesta = urllib.request.urlopen(request)
File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.8/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/lib/python3.8/urllib/request.py", line 640, in http_response
response = self.parent.error(
File "/usr/lib/python3.8/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/usr/lib/python3.8/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
Python code:
import urllib.request
import json
# Debido a cambios en la libreria ahora se deben pasar algunos cabeceros html
paymentInfo = '-u pk_test_51LoUz3BgfM0E8ZiCV8UO79gw8zw7fhSgHUEAVj4wS7igs5D4kKiNsxXGeKQEUhorImNUiCxCNNtidwNkhFPUHP4i0060lsvsbw: -d success_url="http://127.0.0.1:5500/pages/success.html" -d cancel_url="http://127.0.0.1:5500/pages/nosuccess.html" -d "line_items[0][price]"=price_1LvNRkBgfM0E8ZiCTSiaNvNL -d "line_items[0][quantity]"=1 -d mode=subscription -d client_reference_id="123" -d customer_email="[email protected]" -d client_reference_id="tokenized" -d phone_number_collection["enabled"]=true'
res = bytes(paymentInfo,'utf-8')
print(str(type(res)))
request = urllib.request.Request(
'https://api.stripe.com/v1/checkout/sessions',
data=res,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
respuesta = urllib.request.urlopen(request)
print('1. ',respuesta)
cuerpo_respuesta = respuesta.read()
print(cuerpo_respuesta)
#Procesamos la respuesta json
json_respuesta = json.loads(cuerpo_respuesta.decode("utf-8"))
print(json_respuesta)
For asking the question I am using the publishable key of my Stripe account for security reasons, if you try the code with the publishable code it will ask you for the secret key, which I can't give through here. Sorry for the inconvenience
CodePudding user response:
Using the urllib
module, you'll need to set Authorization
header on your request to add the API key as per the Stripe documentation.
request = urllib.request.Request('https://api.stripe.com/v1/checkout/sessions',
data=res,
headers={
'Authorization: Bearer sk_xxx'
}
}
CodePudding user response:
I found the Python API in this link: https://stripe.com/docs/api/checkout/sessions/create?lang=python
By default it appears as curl but when you add lang=python you get the python API code