Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @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

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'onbehalfof', 'C25845632020'

    EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'client_credentials'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', 'YOUR_CLIENT_ID'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_secret', 'YOUR_CLIENT_SECRET'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'scope', 'InvoicingAPI'

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpReq', @success OUT, 'https://preprod-api.myinvois.hasil.gov.my/connect/token', @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        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..


    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'Response Status Code: ' + @iTmp0

    PRINT 'Response Body:'
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    -- Here's a sample response:

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

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0
    DECLARE @access_token nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @access_token OUT, 'access_token'
    DECLARE @expires_in int
    EXEC sp_OAMethod @json, 'IntOf', @expires_in OUT, 'expires_in'
    DECLARE @token_type nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @token_type OUT, 'token_type'
    DECLARE @scope nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @scope OUT, 'scope'

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

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @json


END
GO