(Unicode C++) Moody's REST API - Get OAuth2 Token
Demonstrates how to get an OAuth2 access token for the Moody's REST API.
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkFileAccessW.h>
void ChilkatSample(void)
{
// 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");
CkHttpResponseW *resp = http.PostUrlEncoded(L"https://api.moodys.com/OAuth/Token",req);
if (http.get_LastMethodSuccess() != true) {
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);
}
delete resp;
}
|