Chilkat Online Tools

PureBasic / Salesforce Platform APIs / Registration - Token Exchange

Back to Collection Items

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

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

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

    success.i

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

    CkHttpRequest::ckAddParam(req,"code","<code>")
    CkHttpRequest::ckAddParam(req,"grant_type","authorization_code")

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

    CkHttpRequest::ckAddParam(req,"client_id",CkJsonObject::ckEmit(jsonParam3))

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

    CkHttpRequest::ckAddParam(req,"redirect_uri",CkJsonObject::ckEmit(jsonParam4))

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

    CkHttpRequest::setCkHttpVerb(req, "POST")
    CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded")
    success = CkHttp::ckHttpReq(http,"https://login.salesforce.com{{site}}/services/oauth2/token",req,resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkJsonObject::ckDispose(jsonParam3)
        CkJsonObject::ckDispose(jsonParam4)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)


    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkJsonObject::ckDispose(jsonParam3)
    CkJsonObject::ckDispose(jsonParam4)
    CkHttpResponse::ckDispose(resp)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	--data-urlencode 'code=<code>'
	--data-urlencode 'grant_type=authorization_code'
	--data-urlencode 'client_id={{clientId}}'
	--data-urlencode 'redirect_uri={{redirectUrl}}'
https://login.salesforce.com{{site}}/services/oauth2/token

Postman Collection Item JSON

{
  "name": "Registration - Token Exchange",
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "code",
          "value": "<code>",
          "description": "Auth Code from the Authorize response",
          "type": "text"
        },
        {
          "key": "grant_type",
          "value": "authorization_code",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{clientId}}",
          "type": "text"
        },
        {
          "key": "redirect_uri",
          "value": "{{redirectUrl}}",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{url}}{{site}}/services/oauth2/token",
      "host": [
        "{{url}}{{site}}"
      ],
      "path": [
        "services",
        "oauth2",
        "token"
      ]
    },
    "description": "This exchanges the auth code returned in the Authorize Request for an access token and refresh token. This follows the standard Auth Code flow/Webserver Code Flow pattern. The Code is the return form the Authorization call."
  },
  "response": [
  ]
}