Chilkat Online Tools

PureBasic / Zoho CRM REST APIs / Generate Access Token and Refresh Token

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::setCkHttpVerb(req, "POST")
    CkHttpRequest::setCkPath(req, "/oauth/v2/token")
    CkHttpRequest::setCkContentType(req, "multipart/form-data")

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

    CkHttpRequest::ckAddStringForUpload2(req,"client_id","",jsonFormData1,"utf-8","application/json")

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

    CkHttpRequest::ckAddStringForUpload2(req,"client_secret","",jsonFormData2,"utf-8","application/json")

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

    CkHttpRequest::ckAddStringForUpload2(req,"redirect_uri","",jsonFormData3,"utf-8","application/json")

    CkHttpRequest::ckAddParam(req,"code","{{client-id}}{{client-secret}}{{redirect-uri}}paste_grant_token_here")

    CkHttpRequest::ckAddParam(req,"grant_type","{{client-id}}{{client-secret}}{{redirect-uri}}paste_grant_token_hereauthorization_code")

    resp.i = CkHttp::ckSynchronousRequest(http,"domain.com",443,1,req)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkJsonObject::ckDispose(jsonFormData1)
        CkJsonObject::ckDispose(jsonFormData2)
        CkJsonObject::ckDispose(jsonFormData3)
        ProcedureReturn
    EndIf

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



    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkJsonObject::ckDispose(jsonFormData1)
    CkJsonObject::ckDispose(jsonFormData2)
    CkJsonObject::ckDispose(jsonFormData3)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	--form 'client_id={{client-id}}'
	--form 'client_secret={{client-id}}{{client-secret}}'
	--form 'redirect_uri={{client-id}}{{client-secret}}{{redirect-uri}}'
	--form 'code={{client-id}}{{client-secret}}{{redirect-uri}}paste_grant_token_here'
	--form 'grant_type={{client-id}}{{client-secret}}{{redirect-uri}}paste_grant_token_hereauthorization_code'
https://domain.com/oauth/v2/token

Postman Collection Item JSON

{
  "name": "Generate Access Token and Refresh Token",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "if (responseCode.code === 200)",
          "{",
          "    var jsonData = JSON.parse(responseBody);",
          "",
          "    pm.environment.set(\"refresh-token\", jsonData.refresh_token);",
          "    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": "redirect_uri",
          "value": "{{redirect-uri}}",
          "type": "text"
        },
        {
          "key": "code",
          "value": "paste_grant_token_here",
          "type": "text"
        },
        {
          "key": "grant_type",
          "value": "authorization_code",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{accounts-url}}/oauth/v2/token",
      "host": [
        "{{accounts-url}}"
      ],
      "path": [
        "oauth",
        "v2",
        "token"
      ]
    },
    "description": "### Step 3: Generate Access Token and Refresh Token\n\nOAuth2.0 requests are usually authenticated with an **access token**, which is passed as bearer token. To use this access token, you need to construct a normal HTTP request and include it in an Authorization header along with the value of Bearer."
  },
  "response": [
  ]
}