Issue
I am using Python3.9. I have below curl command :
curl -x proxy1.req:8080 --location --request POST 'https://auth.trst.com/2.0' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'ct_id=33d3e1-4988-9a08-e7773634ba67' \
--data-urlencode 'ct_sec=56GgtoQA8.WIH9nFM3Eh3sT~cwH' \
--data-urlencode 'scope=https://auth.trst.com/settlement/.default' \
--data-urlencode 'grant_type=client_credentials'
Which is returning response as below:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1355 100 1163 100 192 1340 221 --:--:-- --:--:-- --:--:-- 1566{"access_token":"eyJhbGciOiJSUzI1NiIsImtpZCI6Ilg1ZVhrNHh5b2pORnVtMWtsMll0djhkbE5QNC1jNTdkTzZRR1RWQndhTmsiLCJ0eXAiOiJKV9SRSIsInNjcCI6ImRlZmF1bHQiLCJhenBhY3IiOiIxIiwic3ViIjoiZWI3OTU1OTktMzZhMy00SqQe5v9xuYRSfOb_gMgpt7Kez8B0dsnJ_SmT17Hbd7dXLS5p5xTva2iteHp80E2PBYy8jIdtDFcyyC3JSb_d1jzeRrtn5ILlR9eMiMMQa5k65rEXuWYlDpCCtyNS0vcbzA6raxuT1ux8-riCRnGe_TX-VP7FTzPxE7bN1N6N60Ahm7cvzmdDjtKgga0ltybKT2LKtT_hwIwYnuPOdYCGy5jYQ78kaZH86PVSXBuzHw","token_type":"Bearer","not_before":1696859605,"expires_in":3600,"expires_on":1696863205,"resource":"6f4cd1ba-556d-4cb3-ab69-12eac3243387"}
I want to to execute this curl
command in Python
and store the token value in variable so that I can use this variable for further logic in Python
but not sure how to execute this curl
command in Python
.
Solution
As @AbdulNiyasPM said:
curl is an HTTP client intended to used as CLI. If you want to use the same functionality In python use libraries like requests.
So, you can do it this way:
import requests
# Request URL
url = 'https://auth.trst.com/2.0'
# Headers
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
# Payload
payload = {
'ct_id': '33d3e1-4988-9a08-e7773634ba67',
'ct_sec': '56GgtoQA8.WIH9nFM3Eh3sT~cwH',
'scope': 'https://auth.trst.com/settlement/.default',
'grant_type': 'client_credentials'
}
# Proxy URL
proxy_url = 'http://proxy1.baag:8080'
# Make the request with the proxy
response = requests.post(url, headers=headers, data=payload, proxies={'http': proxy_url, 'https': proxy_url})
# Check if the request was successful (HTTP code 200)
if response.status_code == 200:
# Parse the JSON response to get the access token
response_data = response.json()
access_token = response_data.get('access_token')
# TODO: Use the access token as needed
else:
# TODO: Handle the error
Answered By - Diego Borba Answer Checked By - Terry (WPSolving Volunteer)