Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

AzureWebsites OAuth2 Password Flow

See more OAuth2 Examples

Demonstrates how to do OAuth 2.0 password flow for azurewebsites.net.

Chilkat React Native Downloads

React Native
import { Http, HttpRequest, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  const req = new HttpRequest();
  req.httpVerb = 'POST';
  req.path = '/token';
  req.contentType = 'application/x-www-form-urlencoded';
  req.addParam('grant_type', 'password');
  req.addParam('username', 'your_username');
  req.addParam('password', 'your_password');

  const tokenEndpoint = 'https://your_api.azurewebsites.net/token';

  const resp = new HttpResponse();
  try {
    await http.httpReqAsync(tokenEndpoint, 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());

  // Sample JSON response:

  // {
  //   "access_token": "NQGHn ... xTS",
  //   "token_type": "bearer",
  //   "expires_in": 1209599,
  //   "userName": "your_username",
  //   ".issued": "Mon, 27 Apr 2020 23:49:35 GMT",
  //   ".expires": "Mon, 11 May 2020 23:49:35 GMT"
  // }

  let respStatusCode = resp.statusCode;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(resp.header);
    console.log('Failed.');
    return;
  }

  // ----------------------------------
  // Use the OAuth2 token in a request.
  // For example...

  const sbXml = new StringBuilder();
  try {
    sbXml.loadFile('c:/someDir/someXmlFile.xml', 'utf-8');
  } catch {
    console.log('Failed to load the XML file.');
    return;
  }

  // Get the OAuth2 token and use it for authentication
  http.authToken = jResp.stringOf('token');

  const destUrl = 'https://your_api.azurewebsites.net/destinationUrl';
  try {
    await http.httpSbAsync('POST', destUrl, sbXml, 'utf-8', 'application/xml', resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  respStatusCode = resp.statusCode;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(resp.header);
    console.log('Failed.');
    return;
  }

  // Examine the response body
  console.log(resp.bodyStr);
}