Chilkat Online Tools

DataFlex / Zoho CRM REST APIs / Refresh Access Tokens

Back to Collection Items

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vReq
    Handle hoReq
    Variant vJsonFormData1
    Handle hoJsonFormData1
    Variant vJsonFormData2
    Handle hoJsonFormData2
    Variant vJsonFormData3
    Handle hoJsonFormData3
    Variant vResp
    Handle hoResp
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Set ComHttpVerb Of hoReq To "POST"
    Set ComPath Of hoReq To "/oauth/v2/token"
    Set ComContentType Of hoReq To "multipart/form-data"

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonFormData1
    If (Not(IsComObjectCreated(hoJsonFormData1))) Begin
        Send CreateComObject of hoJsonFormData1
    End
    Get pvComObject of hoJsonFormData1 to vJsonFormData1
    Get ComAddStringForUpload2 Of hoReq "client_id" "" vJsonFormData1 "utf-8" "application/json" To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonFormData2
    If (Not(IsComObjectCreated(hoJsonFormData2))) Begin
        Send CreateComObject of hoJsonFormData2
    End
    Get pvComObject of hoJsonFormData2 to vJsonFormData2
    Get ComAddStringForUpload2 Of hoReq "client_secret" "" vJsonFormData2 "utf-8" "application/json" To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonFormData3
    If (Not(IsComObjectCreated(hoJsonFormData3))) Begin
        Send CreateComObject of hoJsonFormData3
    End
    Get pvComObject of hoJsonFormData3 to vJsonFormData3
    Get ComAddStringForUpload2 Of hoReq "refresh_token" "" vJsonFormData3 "utf-8" "application/json" To iSuccess

    Send ComAddParam To hoReq "grant_type" "{{client-id}}{{client-secret}}{{refresh-token}}refresh_token"

    Get pvComObject of hoReq to vReq
    Get ComSynchronousRequest Of hoHttp "domain.com" 443 True vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln iTemp1
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1
    Send Destroy of hoResp


End_Procedure

Curl Command

curl -X POST
	--form 'client_id={{client-id}}'
	--form 'client_secret={{client-id}}{{client-secret}}'
	--form 'refresh_token={{client-id}}{{client-secret}}{{refresh-token}}'
	--form 'grant_type={{client-id}}{{client-secret}}{{refresh-token}}refresh_token'
https://domain.com/oauth/v2/token

Postman Collection Item JSON

{
  "name": "Refresh Access Tokens",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "if (responseCode.code === 200)",
          "{",
          "    var jsonData = JSON.parse(responseBody);",
          "",
          "    pm.environment.set(\"access-token\", jsonData.access_token);",
          "    pm.environment.set(\"expiry-time\", Date.now() + parseInt(jsonData.expires_in));",
          "}"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "client_id",
          "value": "{{client-id}}",
          "type": "text"
        },
        {
          "key": "client_secret",
          "value": "{{client-secret}}",
          "type": "text"
        },
        {
          "key": "refresh_token",
          "value": "{{refresh-token}}",
          "type": "text"
        },
        {
          "key": "grant_type",
          "value": "refresh_token",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{accounts-url}}/oauth/v2/token",
      "host": [
        "{{accounts-url}}"
      ],
      "path": [
        "oauth",
        "v2",
        "token"
      ]
    },
    "description": "### Step 4: Refresh Access Tokens\n\nAs obvious as it is, refresh tokens are used to generate new access tokens when the current ones are past the expiry time."
  },
  "response": [
  ]
}