I have this request using a curl command and I want to translate it to python using the requests library
curl -X POST https://endpoint/prod/api/Translations/start \
-H 'Authorization: Bearer <accessToken>' \
-H 'Content-Type: application/json' \
-d '{ "text": ["first segment to translate.", "second segment to translate."], "sourceLanguageCode": "en", "targetLanguageCode": "de", "model": "general", "useCase": "testing"}'
You can use requests library.
The following curl:
curl -X POST "https://www.magical-website.com/api/v2/token/refresh/" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"refresh": "$REFRESH_TOKEN"
}'
I wrote in python the following way:
import requests
def get_new_token():
url = 'https://www.magical-website.com/api/v2/token/refresh/'
token = constants.THE_TOKEN
payload = f'{{ "refresh": "{token}" }}'
headers = {"accept": "application/json", "Content-Type": "application/json"}
print("Token handling ....")
r = requests.post(url, data=payload, headers=headers)
print(f"Token status: {r.status_code}")
return r.json()['access']
You can try this one.
import requests
url = "https://endpoint/prod/api/Translations/start"
payload = {
"text": ...,
"sourceLanguageCode": ...,
...
}
headers = { "Authorization": "Bearer ...", "Content-Type": "application/json" }
res = requests.post(url, data = payload, headers = headers)
print(res.status_code)
print(res.text)
Related
I am trying to get the sandbox of HSBC's Open Banking to work. The curl command from their documentation gives me a access token only when I disable verification with -k. Note that I downloaded the xyz.der and server.key from my HSBC developer dashboard.
curl -v -k -X POST \
--cert hsbc/qwac_PSP_PI,PSP_AS,PSP_IC,PSP_AI_27_10_2020.der \
--cert-type DER \
--key hsbc/server.key \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json" \
-H "x-fapi-financial-id: test" \
-H "Cache-Control: no-cache" \
-d 'grant_type=client_credentials&scope=accounts&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion=xyz' \
"https://sandbox.hsbc.com/psd2/obie/v3.1/as/token.oauth2"
Since this is working I am trying to do the same with requests but am struggling in how to use the certificates. I know requests supports the cert keyword but it seems I would need to add the other parameters too. Is there a way I can specifics the certificate type and corresponding key?
import requests
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"x-fapi-financial-id": "test",
"Cache-Control": "no-cache",
}
params = {
"grant_type": "client_credentials",
"scope": "accounts",
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
"client_assertion": "xyz",
}
url = "https://sandbox.hsbc.com/psd2/obie/v3.1/as/token.oauth2"
requests.post(url=url,
headers=headers,
params=params,
cert="hsbc/qwac_PSP_PI,PSP_AS,PSP_IC,PSP_AI_27_10_2020.der",
verify=False).json()
Thanks to #KlausD. this works now after converting the .der to .pem with openssl:
openssl x509 -inform der -in qwac_xyz.der -out qwac_xyz.pem
import requests
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"x-fapi-financial-id": "test",
"Cache-Control": "no-cache",
}
params = {
"grant_type": "client_credentials",
"scope": "accounts",
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
"client_assertion": "xyz",
}
url = "https://sandbox.hsbc.com/psd2/obie/v3.1/as/token.oauth2"
requests.post(url=url,
headers=headers,
params=params,
cert=("hsbc/qwac_xyz.pem", "hsbc/server.key"),
verify=False).json()
I can access JWT secured Restful API using curl command as follows
#Get the access Token in a variable ID
export ID=`curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "password": "admin", "rememberMe": true, "username": "admin" }' 'http://localhost:8080/api/authenticate' | jq -r .id_token`
#Use this token to access endpoint
curl 'http://localhost:8080/api/downloads' --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer $ID"
My python script for authentication part and get bearer token is as follows:
import requests
LOGIN_URL = "http://localhost:8080/api/authenticate"
ENDPOINT_URL = 'http://localhost:8080/api/downloads'
PARAMS = {'password': 'admin','rememberMe': True, 'username': 'admin' }
r1 = requests.post(LOGIN_URL, data =PARAMS, headers={"Content-Type": "application/json","Accept": "application/json"})
print(r1)
When i am trying to do the same through python script,Authentication request fails with message <Response [400]>
Help needed !
You are passing a dictionary where you should be passing JSON.
Try using json not data and pass the dictionary:
import requests
LOGIN_URL = "https://httpbin.org/post"
PARAMS = {'password': 'admin','rememberMe': True, 'username': 'admin' }
r1 = requests.post(LOGIN_URL, json=PARAMS, headers={"Content-Type": "application/json","Accept": "application/json"})
print(r1.text)
or pass a string and use data:
import requests
LOGIN_URL = "https://httpbin.org/post"
PARAMS = '{"password": "admin", "rememberMe": true, "username": "admin"}'
r1 = requests.post(LOGIN_URL, data=PARAMS, headers={"Content-Type": "application/json", "Accept": "application/json"})
print(r1.text)
I'm trying to GET requests but I keep getting an response 401
import requests
import json
payload = {
'Accept': 'application/json',
'Authorization': 'Bearer fB5T3IaHjtUXQNb3IH0Lm4epcRHnkAK',
'cache-control': 'no-cache'
}
r = requests.get('https://api.vidoza.net/v1/upload/http/server/', params=payload)
print(r)
this is an example
curl -X GET \
https://api.vidoza.net/v1/upload/http/server \
-H 'Accept: application/json' \
-H 'Authorization: Bearer fB5T3IaHjtUXQNb3IH0Lm4epcRHnkAK' \
-H 'cache-control: no-cache'
What I'm getting
<Response [401]>
Curl Code works python does not
trying to change from curl to python
curl --location --request PUT "https://api.getpostman.com/environments/XXXXX-YYYYYY-ZZZZ-BBBB-AAAA-ZZZZZZ?apikey=12334567890" \
--header "Content-Type: application/json" \
--data "{
\"environment\": {
\"values\": [
{\"key\": \"url\", \"value\": \"http://10.12.30.131\"}
]
}
}"
import requests
import json
url = 'https://api.getpostman.com/environments/XXXXX-YYYYYY-ZZZZ-BBBB-AAAA-ZZZZZZ?apikey=12334567890'
header = {"Content-type": "application/json"}
body = '{\"environment\": { \"name\": \"Prod - Deploy\", \"values\": [ {\"key\": \"url\", \"value\": \"http://10.12.30.131\"}]}}'
response = requests.put(url, data=json.dumps(body), headers=header)
print(response.status_code)
print(response.text)
expect a 200 response
the body should be body = '{"environment\": {"name\": "Prod - Deploy\", "values\": [{"key\": "url\", "value\": "http://10.12.30.131\"}, {"key\": "appPath\", "value\": "footytips-apis-v1\"} ]}}'
I would like to use the Google API to insert a secondary calendar. I've used the Google explanation but can't seem to do it myself. How do you use:
calendar = {
'summary': 'calendarSummary',
'timeZone': 'America/Los_Angeles'
}
created_calendar = service.calendars().insert(body=calendar).execute()
print created_calendar['id']
So if you want to add a secondary calendar this is the curl:
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer here_add_access_token" https://www.googleapis.com/calendar/v3/calendars -d {'summary':'here_calendar_name'}
And in Python is:
import requests
url = "https://www.googleapis.com/calendar/v3/calendars
headers = {
'Content-Type': "application/json",
'Authorization': "Bearer access_token"
}
payload = {
'summary': 'Google Calendar secondary',
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
response = response.text