Sample code for 30+ languages & platforms
Java

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

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.FileAccess;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        HttpRequest 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.setHttpVerb("POST");
        req.setContentType("application/x-www-form-urlencoded");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpReq("https://api.moodys.com/OAuth/Token", req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("status code = " + resp.getStatusCode());
        String responseBody = resp.getBodyStr();
        System.out.println(responseBody);

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