Objective-C / Zoho CRM REST APIs / Refresh Access Tokens
Back to Collection Items
#import <CkoHttp.h>
#import <CkoHttpRequest.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
req.HttpVerb = @"POST";
req.Path = @"/oauth/v2/token";
req.ContentType = @"multipart/form-data";
CkoJsonObject *jsonFormData1 = [[CkoJsonObject alloc] init];
[req AddStringForUpload2: @"client_id" filename: @"" strData: jsonFormData1 charset: @"utf-8" contentType: @"application/json"];
CkoJsonObject *jsonFormData2 = [[CkoJsonObject alloc] init];
[req AddStringForUpload2: @"client_secret" filename: @"" strData: jsonFormData2 charset: @"utf-8" contentType: @"application/json"];
CkoJsonObject *jsonFormData3 = [[CkoJsonObject alloc] init];
[req AddStringForUpload2: @"refresh_token" filename: @"" strData: jsonFormData3 charset: @"utf-8" contentType: @"application/json"];
[req AddParam: @"grant_type" value: @"{{client-id}}{{client-secret}}{{refresh-token}}refresh_token"];
CkoHttpResponse *resp = [http SynchronousRequest: @"domain.com" port: [NSNumber numberWithInt: 443] ssl: YES req: req];
if (http.LastMethodSuccess == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);
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": [
]
}