Chilkat Online Tools

unicodeCpp / Zoho CRM REST APIs / Refresh Access Tokens

Back to Collection Items

#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>

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

    CkHttpW http;
    bool success;

    CkHttpRequestW req;
    req.put_HttpVerb(L"POST");
    req.put_Path(L"/oauth/v2/token");
    req.put_ContentType(L"multipart/form-data");

    CkJsonObjectW jsonFormData1;
    req.AddParam(L"client_id",jsonFormData1.emit());

    CkJsonObjectW jsonFormData2;
    req.AddParam(L"client_secret",jsonFormData2.emit());

    CkJsonObjectW jsonFormData3;
    req.AddParam(L"refresh_token",jsonFormData3.emit());

    req.AddParam(L"grant_type",L"{{client-id}}{{client-secret}}{{refresh-token}}refresh_token");

    CkHttpResponseW *resp = http.SynchronousRequest(L"domain.com",443,true,req);
    if (http.get_LastMethodSuccess() == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"%d\n",resp->get_StatusCode());
    wprintf(L"%s\n",resp->bodyStr());
    delete resp;
    }

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": [
  ]
}