Dart
Dart
citi Developer OAuth2 Client Credentials Grant
See more OAuth2 Examples
Get access token for your application credentials. You can use this for citi APIs which do not require customer credential verification and consent (e.g. Onboarding).Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final http = CkHttp();
// Implements the following CURL command:
// curl --request POST \
// --url https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb \
// --header 'accept: application/json' \
// --user client-id:client-secret \
// --header 'content-type: application/x-www-form-urlencoded' \
// --data 'grant_type=client_credentials&scope=%2Fapi'
http.login = 'client-id';
http.password = 'client-secret';
final req = CkHttpRequest();
req.httpVerb = 'POST';
req.path = '/gcb/api/clientCredentials/oauth2/token/us/gcb';
req.contentType = 'application/x-www-form-urlencoded';
req.addParam('grant_type', 'client_credentials');
req.addParam('scope', '/api');
req.addHeader('accept', 'application/json');
final resp = CkHttpResponse();
try {
http.httpReq('https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb', req, resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final sbResponseBody = CkStringBuilder();
resp.getBodySb(sbResponseBody);
final jResp = CkJsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
print('Response Body:');
print(jResp.emit());
final respStatusCode = resp.statusCode;
print('Response Status Code = $respStatusCode');
if (respStatusCode >= 400) {
print('Response Header:');
print(resp.header);
print('Failed.');
return;
}
try {
jResp.writeFile('qa_data/tokens/citi_client_credentials.json');
} on ChilkatException {
print('Failed to save JSON access token file.');
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "token_type": "bearer",
// "access_token": "AAIkMjdh ... 3fsWb7zJ0s",
// "expires_in": 1800,
// "consented_on": 1584817860,
// "scope": "/api"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
final tokenType = jResp.stringOf('token_type');
final accessToken = jResp.stringOf('access_token');
final expiresIn = jResp.intOf('expires_in');
final consentedOn = jResp.intOf('consented_on');
final scope = jResp.stringOf('scope');
}