Authentication and authorization
Please Note: for all API calls you have to use the access_token. To generate the access_token, you need to request client_secret, client_id, grant_type
from our Delivery Managers.
import json
import requests
def get_client_token():
url_access_token = f'{host}/oauth/token'
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
payload = {
"username": username,
"password": password,
"client_secret": client_secret,
"client_id": client_id,
"grant_type": grant_type
}
files = []
get_token_info = requests.request("POST", url_access_token, headers=headers, data=json.dumps(payload),
files=files)
token_info = json.loads(get_token_info.content.decode('utf8').replace("'", '"'))
access_token = token_info['token_type'] + ' ' + token_info['access_token']
return access_token
access_token = get_client_token()
Example of response:
{
"token_type": "Bearer",
"expires_in": 31536000,
"access_token": "eyJ0eXAiO...",
"refresh_token": "def50200f..."
}
The
get_client_token()
function is defined. This function aims to retrieve the client token for authentication.The
url_access_token
variable is set to the endpoint URL where the access token can be obtained. It appears that host is a variable that should be defined elsewhere.The headers variable is a dictionary specifying the request headers. In this case, the code specifies that the response should be in JSON format.
The payload variable contains the data to be sent as part of the POST request. It includes the
username
,password
,client_secret
,client_id
, andgrant_type
fields.The files variable is an empty list, indicating that no files are being uploaded along with the request.
The
requests.request()
method is used to send a POST request to theurl_access_token
endpoint. It includes the headers, payload (converted to JSON format usingjson.dumps()
), and files.The response is obtained and stored in the
get_token_info
variable.The content of the response is decoded from UTF-8 and converted to a Python dictionary using
json.loads()
. The resulting dictionary is assigned to the token_info variable.The access_token variable is constructed using the token_type and
access_token
values from thetoken_info
dictionary.Finally, the
access_token
is returned from theget_client_token()
function.The
access_token
is assigned the value returned by theget_client_token()
function.
Â