React Native
React Native
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 React Native Downloads
import { Http, HttpRequest, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
// 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';
const req = new HttpRequest();
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');
const resp = new HttpResponse();
try {
await http.httpReqAsync('https://{yourOktaDomain}/oauth2/default/v1/token', req, resp);
} catch {
console.log(http.lastErrorText);
return;
}
const sbResponseBody = new StringBuilder();
resp.getBodySb(sbResponseBody);
const jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
console.log('Response Body:');
console.log(jResp.emit());
const respStatusCode = resp.statusCode;
console.log(`Response Status Code = ${respStatusCode}`);
if (respStatusCode >= 400) {
console.log('Response Header:');
console.log(resp.header);
console.log('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
const accessToken = jResp.stringOf('access_token');
const tokenType = jResp.stringOf('token_type');
const expiresIn = jResp.intOf('expires_in');
const scope = jResp.stringOf('scope');
const idToken = jResp.stringOf('id_token');
}