Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

local req = chilkat.HttpRequest.new()
req:add_param("grant_type", "password")
req:add_param("scope", "api/ratings api/addin rest")
req:add_param("username", "my_username")
req:add_param("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:add_param("client_id", "my_client_id")
req:add_param("client_secret", "my_client_secret")

req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"

local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://api.moodys.com/OAuth/Token", req, resp)
if not success then
    print(http.last_error_text)
    return
end

print("status code = " .. resp.status_code)
local responseBody = resp.body_str
print(responseBody)

-- Save the JSON to a file for future requests.
if resp.status_code == 200 then
    local fac = chilkat.FileAccess.new()
    fac:write_entire_text_file("qa_data/tokens/moodys.json", resp.body_str, "utf-8", false)
end