Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkFileAccessW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttpW http;

    CkHttpRequestW req;
    req.AddParam(L"grant_type",L"password");
    req.AddParam(L"scope",L"api/ratings api/addin rest");
    req.AddParam(L"username",L"my_username");
    req.AddParam(L"password",L"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(L"client_id",L"my_client_id");
    req.AddParam(L"client_secret",L"my_client_secret");

    req.put_HttpVerb(L"POST");
    req.put_ContentType(L"application/x-www-form-urlencoded");

    CkHttpResponseW resp;
    success = http.HttpReq(L"https://api.moodys.com/OAuth/Token",req,resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"status code = %d\n",resp.get_StatusCode());
    const wchar_t *responseBody = resp.bodyStr();
    wprintf(L"%s\n",responseBody);

    // Save the JSON to a file for future requests.
    if (resp.get_StatusCode() == 200) {
        CkFileAccessW fac;
        fac.WriteEntireTextFile(L"qa_data/tokens/moodys.json",resp.bodyStr(),L"utf-8",false);
    }
    }