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..." }
  1. The get_client_token() function is defined. This function aims to retrieve the client token for authentication.

  2. 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.

  3. The headers variable is a dictionary specifying the request headers. In this case, the code specifies that the response should be in JSON format.

  4. The payload variable contains the data to be sent as part of the POST request. It includes the username, password, client_secret, client_id, and grant_type fields.

  5. The files variable is an empty list, indicating that no files are being uploaded along with the request.

  6. The requests.request() method is used to send a POST request to the url_access_token endpoint. It includes the headers, payload (converted to JSON format using json.dumps()), and files.

  7. The response is obtained and stored in the get_token_info variable.

  8. 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.

  9. The access_token variable is constructed using the token_type and access_token values from the token_info dictionary.

  10. Finally, the access_token is returned from the get_client_token() function.

  11. The access_token is assigned the value returned by the get_client_token() function.

Â