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 <CkJwtW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
bool 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...
const wchar_t *apiKey = L"o9rw6Gq0RnqlkfaSqtCMOA";
const wchar_t *apiSecret = L"UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR";
// Create a JWT to authenticate Zoom API requests.
CkJwtW jwt;
CkJsonObjectW jose;
success = jose.UpdateString(L"alg",L"HS256");
success = jose.UpdateString(L"typ",L"JWT");
// Build claims to look like this:
// {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
CkJsonObjectW claims;
success = claims.UpdateString(L"iss",apiKey);
success = claims.UpdateNull(L"aud");
// Set the timestamp of when the JWT was created to now.
int curDateTime = jwt.GenNumericDate(0);
success = claims.AddIntAt(-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)
int oneMonth = 3600 * 24 * 30;
success = claims.AddIntAt(-1,L"exp",curDateTime + oneMonth);
// Produce the smallest possible JWT:
jwt.put_AutoCompact(true);
const wchar_t *strJwt = jwt.createJwt(jose.emit(),claims.emit(),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
CkHttpW http;
// 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
http.SetRequestHeader(L"content-type",L"application/json");
// Adds the "Authorization: Bearer { your_token }" header.
http.put_AuthToken(strJwt);
CkStringBuilderW sbResponseBody;
success = http.QuickGetSb(L"https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1",sbResponseBody);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
CkJsonObjectW jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",jResp.emit());
int respStatusCode = http.get_LastStatus();
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",http.lastHeader());
wprintf(L"Failed.\n");
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
const wchar_t *id = 0;
const wchar_t *first_name = 0;
const wchar_t *last_name = 0;
const wchar_t *email = 0;
int v_type;
int pmi;
const wchar_t *timezone = 0;
int verified;
const wchar_t *created_at = 0;
const wchar_t *last_login_time = 0;
const wchar_t *language = 0;
const wchar_t *phone_number = 0;
const wchar_t *status = 0;
const wchar_t *role_id = 0;
int page_count = jResp.IntOf(L"page_count");
int page_number = jResp.IntOf(L"page_number");
int page_size = jResp.IntOf(L"page_size");
int total_records = jResp.IntOf(L"total_records");
int i = 0;
int count_i = jResp.SizeOfArray(L"users");
while (i < count_i) {
jResp.put_I(i);
id = jResp.stringOf(L"users[i].id");
first_name = jResp.stringOf(L"users[i].first_name");
last_name = jResp.stringOf(L"users[i].last_name");
email = jResp.stringOf(L"users[i].email");
v_type = jResp.IntOf(L"users[i].type");
pmi = jResp.IntOf(L"users[i].pmi");
timezone = jResp.stringOf(L"users[i].timezone");
verified = jResp.IntOf(L"users[i].verified");
created_at = jResp.stringOf(L"users[i].created_at");
last_login_time = jResp.stringOf(L"users[i].last_login_time");
language = jResp.stringOf(L"users[i].language");
phone_number = jResp.stringOf(L"users[i].phone_number");
status = jResp.stringOf(L"users[i].status");
role_id = jResp.stringOf(L"users[i].role_id");
i = i + 1;
}
}