Chilkat Online Tools

SQL Server / Salesforce Platform APIs / Username Password Login - Authorize

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, 'response_type', 'code_credentials'

    DECLARE @jsonParam2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam2 OUT

    EXEC sp_OAMethod @jsonParam2, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', @sTmp0

    DECLARE @jsonParam3 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam3 OUT

    EXEC sp_OAMethod @jsonParam3, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'redirect_uri', @sTmp0

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Auth-Request-Type', 'Named-User'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Authorization', 'Basic <username:password>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://login.salesforce.com{{site}}/services/oauth2/authorize', @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
        EXEC @hr = sp_OADestroy @jsonParam2
        EXEC @hr = sp_OADestroy @jsonParam3
        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
    EXEC @hr = sp_OADestroy @jsonParam2
    EXEC @hr = sp_OADestroy @jsonParam3


END
GO

Curl Command

curl -X POST
	-H "Auth-Request-Type: Named-User"
	-H "Authorization: Basic <username:password>"
	--data-urlencode 'response_type=code_credentials'
	--data-urlencode 'client_id={{clientId}}'
	--data-urlencode 'redirect_uri={{redirectUrl}}'
https://login.salesforce.com{{site}}/services/oauth2/authorize

Postman Collection Item JSON

{
  "name": "Username Password Login - Authorize",
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Auth-Request-Type",
        "value": "Named-User",
        "description": "Required for Username Password Login"
      },
      {
        "key": "Authorization",
        "value": "Basic <username:password>",
        "description": "Base 64 encoded username:password"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "response_type",
          "value": "code_credentials",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{clientId}}",
          "type": "text"
        },
        {
          "key": "redirect_uri",
          "value": "{{redirectUrl}}",
          "type": "text"
        },
        {
          "key": "scope",
          "value": "",
          "description": "Comma seperated list of scopes, optional",
          "type": "text",
          "disabled": true
        }
      ]
    },
    "url": {
      "raw": "{{url}}{{site}}/services/oauth2/authorize",
      "host": [
        "{{url}}{{site}}"
      ],
      "path": [
        "services",
        "oauth2",
        "authorize"
      ]
    },
    "description": "This request allows named users to use their username and password to get an access token and refresh token from Salesforce in a headless manner. There are two headers that must be included, the first is the `Auth-Request-Type` which must be set to `Named-User`, and then there is the `Authorization` Basic header which contains the base 64 encoded username:password of the user.\n\nThe result of this API call, is an Auth Code, which is then exchanged with another call at the token endpoint."
  },
  "response": [
  ]
}