Chilkat Online Tools

unicodeCpp / DocuSign REST API / 01 Authorize Code Grant Access Token

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;

    CkJsonObjectW jsonParam1;
    req.AddParam(L"code",jsonParam1.emit());
    req.AddParam(L"grant_type",L"authorization_code");

    req.AddHeader(L"Authorization",L"Basic {{encodedKeys}}");

    CkHttpResponseW *resp = http.PostUrlEncoded(L"https://{{hostenv}}/oauth/token",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
	-H "Authorization: Basic {{encodedKeys}}"
	--data-urlencode 'code={{codeFromUrl}}'
	--data-urlencode 'grant_type=authorization_code'
https://{{hostenv}}/oauth/token

Postman Collection Item JSON

{
  "name": "01 Authorize Code Grant Access Token",
  "event": [
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "var jsonData = JSON.parse(responseBody);",
          "postman.setEnvironmentVariable(\"accessToken\", jsonData.access_token);",
          "var jsonData = JSON.parse(responseBody);",
          "postman.setEnvironmentVariable(\"refreshToken\", jsonData.refresh_token);"
        ]
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Authorization",
        "value": "Basic {{encodedKeys}}"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "code",
          "value": "{{codeFromUrl}}"
        },
        {
          "key": "grant_type",
          "value": "authorization_code"
        }
      ]
    },
    "url": {
      "raw": "https://{{hostenv}}/oauth/token",
      "protocol": "https",
      "host": [
        "{{hostenv}}"
      ],
      "path": [
        "oauth",
        "token"
      ]
    }
  },
  "response": [
  ]
}