Unicode C
Unicode C
Zoom API - Create JWT to Authenticate API Requests
See more Zoom Examples
Creates a JWT for the Zoom API.Chilkat Unicode C Downloads
#include <C_CkJwtW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
const wchar_t *apiKey;
const wchar_t *apiSecret;
HCkJwtW jwt;
HCkJsonObjectW jose;
HCkJsonObjectW claims;
int curDateTime;
int oneMonth;
const wchar_t *strJwt;
HCkHttpW http;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *id;
const wchar_t *first_name;
const wchar_t *last_name;
const wchar_t *email;
int v_type;
int pmi;
const wchar_t *timezone;
int verified;
const wchar_t *created_at;
const wchar_t *last_login_time;
const wchar_t *language;
const wchar_t *phone_number;
const wchar_t *status;
const wchar_t *role_id;
int page_count;
int page_number;
int page_size;
int total_records;
int i;
int count_i;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Use your API key and secret here...
apiKey = L"o9rw6Gq0RnqlkfaSqtCMOA";
apiSecret = L"UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR";
// Create a JWT to authenticate Zoom API requests.
jwt = CkJwtW_Create();
jose = CkJsonObjectW_Create();
success = CkJsonObjectW_UpdateString(jose,L"alg",L"HS256");
success = CkJsonObjectW_UpdateString(jose,L"typ",L"JWT");
// Build claims to look like this:
// {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
claims = CkJsonObjectW_Create();
success = CkJsonObjectW_UpdateString(claims,L"iss",apiKey);
success = CkJsonObjectW_UpdateNull(claims,L"aud");
// Set the timestamp of when the JWT was created to now.
curDateTime = CkJwtW_GenNumericDate(jwt,0);
success = CkJsonObjectW_AddIntAt(claims,-1,L"iat",curDateTime);
// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 month(3600 * 24 * 30 seconds)
oneMonth = 3600 * 24 * 30;
success = CkJsonObjectW_AddIntAt(claims,-1,L"exp",curDateTime + oneMonth);
// Produce the smallest possible JWT:
CkJwtW_putAutoCompact(jwt,TRUE);
strJwt = CkJwtW_createJwt(jwt,CkJsonObjectW_emit(jose),CkJsonObjectW_emit(claims),apiSecret);
wprintf(L"%s\n",strJwt);
// Let's test the JWT to by sending the following request:
// curl --request GET \
// --url 'https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1' \
// --header 'authorization: Bearer { your_token }' \
// --header 'content-type: application/json
http = CkHttpW_Create();
// Implements the following CURL command:
// curl --request GET \
// --url 'https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1' \
// --header 'authorization: Bearer { your_token }' \
// --header 'content-type: application/json
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
CkHttpW_SetRequestHeader(http,L"content-type",L"application/json");
// Adds the "Authorization: Bearer { your_token }" header.
CkHttpW_putAuthToken(http,strJwt);
sbResponseBody = CkStringBuilderW_Create();
success = CkHttpW_QuickGetSb(http,L"https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1",sbResponseBody);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkJwtW_Dispose(jwt);
CkJsonObjectW_Dispose(jose);
CkJsonObjectW_Dispose(claims);
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpW_getLastStatus(http);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpW_lastHeader(http));
wprintf(L"Failed.\n");
CkJwtW_Dispose(jwt);
CkJsonObjectW_Dispose(jose);
CkJsonObjectW_Dispose(claims);
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// Sample output:
// {
// "page_count": 1,
// "page_number": 1,
// "page_size": 30,
// "total_records": 1,
// "users": [
// {
// "id": "s8uAiMJiRmS_-eu1yOhKlg",
// "first_name": "Joe",
// "last_name": "Example",
// "email": "joe@example.com",
// "type": 1,
// "pmi": 5224934114,
// "timezone": "America/Chicago",
// "verified": 1,
// "created_at": "2021-07-30T11:56:37Z",
// "last_login_time": "2021-07-30T11:56:37Z",
// "language": "en-US",
// "phone_number": "",
// "status": "active",
// "role_id": "0"
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
page_count = CkJsonObjectW_IntOf(jResp,L"page_count");
page_number = CkJsonObjectW_IntOf(jResp,L"page_number");
page_size = CkJsonObjectW_IntOf(jResp,L"page_size");
total_records = CkJsonObjectW_IntOf(jResp,L"total_records");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"users");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
id = CkJsonObjectW_stringOf(jResp,L"users[i].id");
first_name = CkJsonObjectW_stringOf(jResp,L"users[i].first_name");
last_name = CkJsonObjectW_stringOf(jResp,L"users[i].last_name");
email = CkJsonObjectW_stringOf(jResp,L"users[i].email");
v_type = CkJsonObjectW_IntOf(jResp,L"users[i].type");
pmi = CkJsonObjectW_IntOf(jResp,L"users[i].pmi");
timezone = CkJsonObjectW_stringOf(jResp,L"users[i].timezone");
verified = CkJsonObjectW_IntOf(jResp,L"users[i].verified");
created_at = CkJsonObjectW_stringOf(jResp,L"users[i].created_at");
last_login_time = CkJsonObjectW_stringOf(jResp,L"users[i].last_login_time");
language = CkJsonObjectW_stringOf(jResp,L"users[i].language");
phone_number = CkJsonObjectW_stringOf(jResp,L"users[i].phone_number");
status = CkJsonObjectW_stringOf(jResp,L"users[i].status");
role_id = CkJsonObjectW_stringOf(jResp,L"users[i].role_id");
i = i + 1;
}
CkJwtW_Dispose(jwt);
CkJsonObjectW_Dispose(jose);
CkJsonObjectW_Dispose(claims);
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}