Sample code for 30+ languages & platforms
Dart

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 Dart Downloads

Dart
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();

  final req = CkHttpRequest();
  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';

  final resp = CkHttpResponse();
  try {
    http.httpReq('https://api.moodys.com/OAuth/Token', req, resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('status code = ${resp.statusCode}');
  final responseBody = resp.bodyStr;
  print(responseBody);

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