DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vReq
Handle hoReq
Variant vResp
Handle hoResp
Handle hoJson
String sAccess_token
Integer iExpires_in
String sToken_type
String sScope
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
If (Not(IsComObjectCreated(hoReq))) Begin
Send CreateComObject of hoReq
End
Send ComAddHeader To hoReq "onbehalfof" "C25845632020"
Send ComAddParam To hoReq "grant_type" "client_credentials"
Send ComAddParam To hoReq "client_id" "YOUR_CLIENT_ID"
Send ComAddParam To hoReq "client_secret" "YOUR_CLIENT_SECRET"
Send ComAddParam To hoReq "scope" "InvoicingAPI"
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoReq to vReq
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp "https://preprod-api.myinvois.hasil.gov.my/connect/token" vReq vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
// 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..
Get ComStatusCode Of hoResp To iTemp1
Showln "Response Status Code: " iTemp1
Showln "Response Body:"
Get ComBodyStr Of hoResp To sTemp1
Showln sTemp1
// Here's a sample response:
// {
// "access_token": "eyJhbGciOiJSUzI1...",
// "expires_in": 3600,
// "token_type": "Bearer",
// "scope": "InvoicingAPI"
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoJson sTemp1 To iSuccess
Get ComStringOf Of hoJson "access_token" To sAccess_token
Get ComIntOf Of hoJson "expires_in" To iExpires_in
Get ComStringOf Of hoJson "token_type" To sToken_type
Get ComStringOf Of hoJson "scope" To sScope
// To use an access token in a MyInvois API call, see Using a MyInvois Access Token in an API Request
End_Procedure