authorize method

Future<AccessToken> authorize({
  1. required String publicKey,
  2. required String privateKey,
})

Retrieves the access token for the calling account.

Implementation

Future<AccessToken> authorize({required String publicKey, required String privateKey}) async {
  Map<String, String> headers = {
    //"Authorization": "Bearer $apiKey",
    "Content-Type": "application/x-www-form-urlencoded",
    "Access-Control-Allow-Origin": "*", // Required for CORS support to work
  };

  Uri endpoint;
  if(secure) {
    endpoint = Uri.https(
        baseUrl, "/token");
  }
  else {
    endpoint = Uri.http(
        baseUrl, "/token");
  }

  var data = { 'grant_type' : 'client_credentials', 'client_id' : publicKey, 'client_secret': privateKey };
  var response = await http.post(endpoint, headers: headers, body:data);

  if (response.statusCode == 200 || response.statusCode == 201) {

    var token = AccessToken.fromJson(jsonDecode(const Utf8Decoder().convert(response.bodyBytes)));
    token.calculateExpiration();
    return token;
  }
  else {
    var error = ServerError.fromJson(jsonDecode(response.body));
    throw AuthException(statusCode: response.statusCode, message: "${error.error} - ${error.errorDescription}");
  }

}