Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Req
oleobject loo_Resp
string ls_ResponseBody
oleobject loo_Fac

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

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

loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpReq("https://api.moodys.com/OAuth/Token",loo_Req,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Resp
    return
end if

Write-Debug "status code = " + string(loo_Resp.StatusCode)
ls_ResponseBody = loo_Resp.BodyStr
Write-Debug ls_ResponseBody

// Save the JSON to a file for future requests.
if loo_Resp.StatusCode = 200 then
    loo_Fac = create oleobject
    li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

    loo_Fac.WriteEntireTextFile("qa_data/tokens/moodys.json",loo_Resp.BodyStr,"utf-8",0)
end if



destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_Fac