Sample code for 30+ languages & platforms
React Native

Moody's REST API - Get OAuth2 Token

See more Moody's Examples

Demonstrates how to get an OAuth2 access token for the Moody's REST API.

Chilkat React Native Downloads

React Native
import { FileAccess, Http, HttpRequest, HttpResponse } 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();

  const req = new HttpRequest();
  req.addParam('grant_type', 'password');
  req.addParam('scope', 'api/ratings api/addin rest');
  req.addParam('username', 'my_username');
  req.addParam('password', 'my_password');
  // I have no idea of where to get the client_id or client_secret.
  // When you create a Moody's App, it only provides an "API Key".
  req.addParam('client_id', 'my_client_id');
  req.addParam('client_secret', 'my_client_secret');

  req.httpVerb = 'POST';
  req.contentType = 'application/x-www-form-urlencoded';

  const resp = new HttpResponse();
  try {
    await http.httpReqAsync('https://api.moodys.com/OAuth/Token', req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  console.log(`status code = ${resp.statusCode}`);
  const responseBody = resp.bodyStr;
  console.log(responseBody);

  // Save the JSON to a file for future requests.
  if (resp.statusCode === 200) {
    const fac = new FileAccess();
    fac.writeEntireTextFile('qa_data/tokens/moodys.json', resp.bodyStr, 'utf-8', false);
  }
}