Chilkat Online Tools

SQL Server / api-seguridad.sunat.gob.pe / Get Token

Back to Collection Items

-- 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)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    DECLARE @success int

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

    EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'password'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'scope', 'https://api-cpe.sunat.gob.pe'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', '99993656-9999-9999-90ad-9999d06d9999'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_secret', 'u3Hhp5vbb5GubHPdQ161sQ=='
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'username', '20307150981SOPORTE1'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'password', '12345678'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://api-seguridad.sunat.gob.pe/v1/clientessol/99993656-9999-9999-90ad-9999d06d9999/oauth2/token/', @req
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req


END
GO

Curl Command

curl -X POST
	--data-urlencode 'grant_type=password'
	--data-urlencode 'scope=https://api-cpe.sunat.gob.pe'
	--data-urlencode 'client_id=99993656-9999-9999-90ad-9999d06d9999'
	--data-urlencode 'client_secret=u3Hhp5vbb5GubHPdQ161sQ=='
	--data-urlencode 'username=20307150981SOPORTE1'
	--data-urlencode 'password=12345678'
https://api-seguridad.sunat.gob.pe/v1/clientessol/99993656-9999-9999-90ad-9999d06d9999/oauth2/token/

Postman Collection Item JSON

{
  "name": "Get Token",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "grant_type",
          "value": "password",
          "type": "text"
        },
        {
          "key": "scope",
          "value": "https://api-cpe.sunat.gob.pe",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "99993656-9999-9999-90ad-9999d06d9999",
          "type": "text"
        },
        {
          "key": "client_secret",
          "value": "u3Hhp5vbb5GubHPdQ161sQ==",
          "type": "text"
        },
        {
          "key": "username",
          "value": "20307150981SOPORTE1",
          "type": "text"
        },
        {
          "key": "password",
          "value": "12345678",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "https://api-seguridad.sunat.gob.pe/v1/clientessol/99993656-9999-9999-90ad-9999d06d9999/oauth2/token/",
      "protocol": "https",
      "host": [
        "api-seguridad",
        "sunat",
        "gob",
        "pe"
      ],
      "path": [
        "v1",
        "clientessol",
        "99993656-9999-9999-90ad-9999d06d9999",
        "oauth2",
        "token",
        ""
      ]
    }
  },
  "response": [
  ]
}