missing keyword parameter is clearly included in api call - amadeus

I'm trying to set up a basic API call to Amadeus. All the correct parameters are included and I'm grabbing a fresh token for each call.
It's a React/Flask application.
Here's my endpoint in app/routes.py:
#app.route('/search', methods=['GET'])
#cross_origin(origin='*')
def get_flights():
api_key = os.environ.get('amadeus_api_key', None)
api_secret = os.environ.get('amadeus_api_secret', None)
# first, you must get an access token using your Amadeus credentials
token_request = requests.post(
'https://test.api.amadeus.com/v1/security/oauth2/token',
data = {
'grant_type': 'client_credentials',
'client_id': api_key,
'client_secret': api_secret
}
)
token = token_request.json()['access_token']
bearer = 'Bearer {}'.format(token)
locations = requests.get(
'https://test.api.amadeus.com/v1/reference-data/locations',
headers = {
'Authorization': bearer
},
data = {
'subType': 'AIRPORT',
'keyword': 'BOS'
}
)
print(locations.json())
# example:
# https://test.api.amadeus.com/v1/reference-data/locations
# ?subType=AIRPORT&keyword=BOS
return jsonify({'token': token})
Here's the error:
{'errors': [{'status': 400, 'code': 32171, 'title': 'MANDATORY DATA MISSING', 'detail': 'Missing mandatory query parameter', 'source': {'parameter': 'keyword'}}]}
As you can see in the /search endpoint, the keyword parameter is clearly included.
What gives? Am I missing something?

The request to get the locations is not correctly built as it is sending subType and keyword as body instead of query parameters. According to requests documentation, you need to use params:
locations = requests.get(
'https://test.api.amadeus.com/v1/reference-data/locations',
headers = {
'Authorization': bearer
},
params = {
'subType': 'AIRPORT',
'keyword': 'BOS'
}
)

Related

Need to access to the Bynder API

I am trying to connect to the Bynder API using Python and the documentation https://bynder.docs.apiary.io/ but I can make it to connect to the Analytics endpoint https://bynder.docs.apiary.io/#reference/analytics because I received a 401 response
I have the following code :
from bynder_sdk import BynderClient
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'Allowed domain for cross-domain requests. Only required if any domains were specified in \'Set HTTP access control (CORS)\' for the OAuth application.',
'Authorization': 'Basic b64("abcd":"abcd")'
}
bynder_client = BynderClient(
domain='abcd.com',
redirect_uri='https://abcd.com/v7/analytics/api/v1/asset/view',
client_id='abcd',
client_secret='abcd',
scopes ="offline analytics.api:read",
grant_type="client_credentials"
)
print(bynder_client.get_authorization_url())
print(bynder_client.get_authorization_url()[1])
params = {"limit":"100", "fromDateTime":"2022-01-01T01:00","toDateTime":"2022-06-01T01:00" }
api_call_headers = {'Authorization': 'Token ' + bynder_client.get_authorization_url()[1]}
api_call_response = requests.get("https://abcd.abcd.com/v7/analytics/api/v1/asset/view", headers=api_call_headers, params=params, verify=False)
can someone help me to understand how to Autorise using OAuth 2.0 the Client ID and Client Secret and use the Analytics endpoint? I have all the details in the bynder_client = BynderClient()
Thanks
For anyone reference this his how I ended up making my code work:
endpoint_api_url="https://abcd.abcd.com/v7/analytics/api/v1/asset/download"
auth_server_url = "https://abcd.abcd.com/v6/authentication/oauth2/token"
client_id = 'abcd'
client_secret='abcd'
token_req_payload = {'grant_type': 'client_credentials'}
token_response = requests.post(auth_server_url,
data=token_req_payload, verify=False, allow_redirects=False,
auth=(client_id, client_secret))
print(token_response.status_code)
if token_response.status_code ==200:
print("Successfuly obtained a new token")
print(token_response.text)
tokens = json.loads(token_response.text)
token = tokens['access_token']
print(token)
else:
print("Failed to obtain token from the OAuth 2.0 server", file=sys.stderr)
sys.exit(1)
params = {"limit":"100", "fromDateTime":"2022-01-01T01:00","toDateTime":"2022-06-01T01:00" }
api_call_headers = {'Authorization': 'Bearer ' + token}
api_call_response = requests.get(endpoint_api_url, headers=api_call_headers, params=params, verify=False)
print(api_call_response.text)

Getting "unsupported_grant_type" when trying to refresh xero API token using python

Getting "unsupported_grant_type" when trying to refresh token using python
Hi,
I've been trying to get a new access token & refresh token using an existing refresh token that I have. I am following the documentation as stated on the website https://developer.xero.com/documentation/oauth2/auth-flow but I keep getting an error saying "unsupported_grant_type" although I do define grant_type = refresh_token. Here's my code, any help would be greatly appreciated.
import json
from base64 import b64encode
client_id = xxx
client_secret = xxx
RefreshToken = xxx
b64_id_secret = b64encode(client_id + ':' + client_secret)
def XeroRefreshToken(refresh_token):
token_refresh_url = 'https://identity.xero.com/connect/token'
response = requests.post(token_refresh_url,
headers = {
'Authorization' : 'Basic ' + b64_id_secret,
'Content-Type': 'application/x-www-form-urlencoded'
},
data = {
'grant_type' : 'refresh_token',
'refresh_token' : refresh_token
})
json_response = response.json()
print(json_response)
new_refresh_token = json_response['refresh_token']
XeroRefreshToken(RefreshToken)

Odoo controllers avoid json-rpc when type="json"

I've the following route:
#http.route([
'/whatever/create'
], auth="none", type='json', methods=['POST'], csrf=False)
which I'm using to send a post request with json data on its body.
Is there any way to avoid using json-rpc responses on routes of type="json"? I would like to just answer plain json.
If it is not possible, is there any way to get the json data placed on the body request by using `type="http"?
#http.route('/whatever/create', auth='public', methods=['POST'], type='http')
def index(self, **kw):
data = request.httprequest.data
return 'success'
Above code defined in Odoo
url = "http://localhost:8069/whatever/create"
param = {
"type_operation": "PTI",
"label": "",
}
headers = {'Content-type': 'text/plain'}
r = requests.post(url, data=json.dumps(param), headers=headers)
Above code i have requested from a py file
While sending request you should change Content-type
'Content-type': 'application/json' --- > 'Content-type': 'text/plain'
Also while returning it only accept String
return {'status': 'success'} ---> return 'success'

ADF Create Pipeline Run - Parameters

I need to trigger a ADF Pipeline via REST API and pass a parameter in order to execute the pipeline for the given ID (parameter).
With sparse documentation around this, I am unable to figure out how to pass parameters to the URL
Sample:
https://management.azure.com/subscriptions/asdc57878-77fg-fb1e8-7b06-7b0698bfb1e8/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/df-datafactory-dev/pipelines/pl_StartProcessing/createRun?api-version=2018-06-01
I tried to send parmaters in the request body but I get the following message depending on how params are sent
{
"message": "The request entity's media type 'text/plain' is not supported for this resource."
}
I tried using python requests :
import requests
url = "https://management.azure.com/subscriptions/adsad-asdasd-adasd-adasda-adada/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/datafactory-dev/pipelines/pl_Processing/createRun?api-version=2018-06-01"
payload = " \"parameters\": {\r\n “stateID”: “78787878”\r\n}"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer adsasdasdsad'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
I tried to put the parameter in the payload (body)
Paramters can be passed within body
python sample:
import requests
url = "https://management.azure.com/subscriptions/adsad-asdasd-adasd-adasda-adada/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/datafactory-dev/pipelines/pl_Processing/createRun?api-version=2018-06-01"
payload = "{\"stateID\":1200}"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer adsasdasdsad'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
You have to use a parameter name as post
url = "https://management.azure.com/subscriptions/adsad-asdasd-adasd-adasda-adada/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/datafactory-dev/pipelines/pl_Processing/createRun?api-version=2018-06-01 -d '{"stateID"="78787878"}'
microsoft docs for your reference :
https://learn.microsoft.com/en-us/rest/api/datafactory/pipelines/create-run
You have to pass them as the POST body.
To pass more than one parameter the body this looks like:
{
"param1": "param1value"
,"param2":"param2value"
}

Unable to generate OAuth 2.0 Access Token from Office365 via JavaScript

I'm trying to pull an access token from Office365's /token identity platform endpoint via OAuth 2.0 client credentials grant flow. I have my app registered, the client ID & secret, etc...
I can make the POST request in Postman and receive the access token without issue:
However, when I try the POST request via JavaScript (by way of Google Apps Script), I receive an error message: AADSTS900144: The request body must contain the following parameter: 'grant_type'
I've already Google'd this error and found a bunch of different solutions, and have tried implementing them to no avail. I imagine this has to do with the URL encoding, but cannot figure it out.
Code:
function getO365() {
// POST Request (To get Access Token)
var tenantID = 'longstringhere'
var appID = 'longstringhere'
var appSecret = 'longstringhere'
var graphScore = 'https://graph.microsoft.com/.default'
var url = 'https://login.microsoftonline.com/' + tenantID + '/oauth2/v2.0/token'
var data = {
'client_id': appID,
'scope': graphScore,
'client_secret': appSecret,
'grant_type': 'client_credentials'
};
var postOptions = {
'method': 'POST',
'headers': {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
'body': data,
'redirect': 'follow'
};
var authToken = UrlFetchApp.fetch(url, postOptions);
}
The only real difference between my code and the JavaScript Fetch code I pulled off of Postman is:
var urlencoded = new URLSearchParams();
urlencoded.append("client_id", "longstringhere");
urlencoded.append("scope", "https://graph.microsoft.com/.default");
urlencoded.append("client_secret", "longstringhere");
urlencoded.append("grant_type", "client_credentials");
When I try to use URLSearchParams in Google Apps Script, I keep getting this error: ReferenceError: URLSearchParams is not defined
Any ideas? Thanks in advance!
This was resolved by changing 'body' to 'payload' for UrlFetchApp per the documentation. Edited code to reflect the change. Credit to #TheMaster for pointing out my mistake.
'payload': data,//from 'body': data,