Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpRequest.pb"

Procedure ChilkatExample()

    success.i = 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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::ckAddHeader(req,"onbehalfof","C25845632020")

    CkHttpRequest::ckAddParam(req,"grant_type","client_credentials")
    CkHttpRequest::ckAddParam(req,"client_id","YOUR_CLIENT_ID")
    CkHttpRequest::ckAddParam(req,"client_secret","YOUR_CLIENT_SECRET")
    CkHttpRequest::ckAddParam(req,"scope","InvoicingAPI")

    CkHttpRequest::setCkHttpVerb(req, "POST")
    CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded")

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpReq(http,"https://preprod-api.myinvois.hasil.gov.my/connect/token",req,resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    ; 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..

    Debug "Response Status Code: " + Str(CkHttpResponse::ckStatusCode(resp))
    Debug "Response Body:"
    Debug CkHttpResponse::ckBodyStr(resp)

    ; Here's a sample response:

    ; {
    ;   "access_token": "eyJhbGciOiJSUzI1...",
    ;   "expires_in": 3600,
    ;   "token_type": "Bearer",
    ;   "scope": "InvoicingAPI"
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
    access_token.s = CkJsonObject::ckStringOf(json,"access_token")
    expires_in.i = CkJsonObject::ckIntOf(json,"expires_in")
    token_type.s = CkJsonObject::ckStringOf(json,"token_type")
    scope.s = CkJsonObject::ckStringOf(json,"scope")

    ; To use an access token in a MyInvois API call, see Using a MyInvois Access Token in an API Request


    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure