PowerBuilder
PowerBuilder
MyInvois Malaysia Login as Intermediary System
See more Malaysia MyInvois Examples
Demonstrates how to get an OAuth2 access token with an intermediary that is representing a taxpayer (acting on behalf of a specific taxpayer). The OAuth2 access token can then be used to access MyInvois protected APIs.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Req
oleobject loo_Resp
oleobject loo_Json
string ls_Access_token
integer li_Expires_in
string ls_Token_type
string ls_Scope
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Sends the following HTTP POST to get a MyInvois OAUth2 access token using client_credentials
// POST /connect/token HTTP/1.1
// Host: preprod-api.myinvois.hasil.gov.my
// Accept: */*
// Content-Length: <<variable>>
// Content-Type: application/x-www-form-urlencoded
// onbehalfof: C25845632020
//
// client_id={YOUR_CLIENT_ID}&client_secret={YOUR_CLIENT_SECRET}&grant_type=client_credentials&scope=InvoicingAPI
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.AddHeader("onbehalfof","C25845632020")
loo_Req.AddParam("grant_type","client_credentials")
loo_Req.AddParam("client_id","YOUR_CLIENT_ID")
loo_Req.AddParam("client_secret","YOUR_CLIENT_SECRET")
loo_Req.AddParam("scope","InvoicingAPI")
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://preprod-api.myinvois.hasil.gov.my/connect/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
// Note: The returned access token is valid for a short amount of time. Perhaps 1 hour.
// The access token is used in the "Authorization: Bearer <access_token>" header in subsequent requests until it expires.
// Your application would then need to get a new access token, and so on..
Write-Debug "Response Status Code: " + string(loo_Resp.StatusCode)
Write-Debug "Response Body:"
Write-Debug loo_Resp.BodyStr
// Here's a sample response:
// {
// "access_token": "eyJhbGciOiJSUzI1...",
// "expires_in": 3600,
// "token_type": "Bearer",
// "scope": "InvoicingAPI"
// }
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.Load(loo_Resp.BodyStr)
ls_Access_token = loo_Json.StringOf("access_token")
li_Expires_in = loo_Json.IntOf("expires_in")
ls_Token_type = loo_Json.StringOf("token_type")
ls_Scope = loo_Json.StringOf("scope")
// To use an access token in a MyInvois API call, see Using a MyInvois Access Token in an API Request
destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_Json