Dart
Dart
Okta: Use the Resource Owner Password Flow
See more Okta OAuth/OIDC Examples
Demonstrates how to get an access token using the Resource Owner Password Flow.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://{yourOktaDomain}/oauth2/default/v1/token \
// --header 'accept: application/json' \
// --user "client_id:client_secret" \
// --header 'content-type: application/x-www-form-urlencoded' \
// --data 'grant_type=password&username=myUserEmail&password=myPassword&scope=openid'
http.login = 'client_id';
http.password = 'client_secret';
final req = CkHttpRequest();
req.httpVerb = 'POST';
req.path = '/oauth2/default/v1/token';
req.contentType = 'application/x-www-form-urlencoded';
req.addParam('grant_type', 'password');
req.addParam('username', 'myUserEmail');
req.addParam('password', 'myPassword');
req.addParam('scope', 'openid');
req.addHeader('accept', 'application/json');
final resp = CkHttpResponse();
try {
http.httpReq('https://{yourOktaDomain}/oauth2/default/v1/token', 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;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "access_token": "eyJraWQiOi ... jmiHD7wY9_gQ",
// "token_type": "Bearer",
// "expires_in": 3600,
// "scope": "openid",
// "id_token": "eyJraWQiOiJ ... W7KkWiPJnUSMoGw"
// }
// 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 accessToken = jResp.stringOf('access_token');
final tokenType = jResp.stringOf('token_type');
final expiresIn = jResp.intOf('expires_in');
final scope = jResp.stringOf('scope');
final idToken = jResp.stringOf('id_token');
}