Sample code for 30+ languages & platforms
Perl

Zoom API - Create JWT to Authenticate API Requests

See more Zoom Examples

Creates a JWT for the Zoom API.

Chilkat Perl Downloads

Chilkat2 Perl
use strict;
use warnings;
use Chilkat2;

# 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...
my $apiKey = 'o9rw6Gq0RnqlkfaSqtCMOA';
my $apiSecret = 'UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR';

# Create a JWT to authenticate Zoom API requests.
my $jwt = Chilkat2::Jwt->new;

my $jose = Chilkat2::JsonObject->new;
my $success = $jose->UpdateString('alg', 'HS256');
$success = $jose->UpdateString('typ', 'JWT');

# Build claims to look like this:
# {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
my $claims = Chilkat2::JsonObject->new;
$success = $claims->UpdateString('iss', $apiKey);
$success = $claims->UpdateNull('aud');

# Set the timestamp of when the JWT was created to now.
my $curDateTime = $jwt->GenNumericDate(0);
$success = $claims->AddIntAt(-1, 'iat', $curDateTime);

# Set the timestamp defining an expiration time (end time) for the token
# to be now + 1 month(3600 * 24 * 30 seconds)
my $oneMonth = 3600 * 24 * 30;
$success = $claims->AddIntAt(-1, 'exp', $curDateTime + $oneMonth);

# Produce the smallest possible JWT:
$jwt->AutoCompact(1);

my $strJwt = $jwt->CreateJwt($jose->Emit(), $claims->Emit(), $apiSecret);

print $strJwt, "\n";

# 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

my $http = Chilkat2::Http->new;

# 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('content-type', 'application/json');
# Adds the "Authorization: Bearer { your_token }" header.
$http->AuthToken($strJwt);

my $sbResponseBody = Chilkat2::StringBuilder->new;
$success = $http->QuickGetSb('https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1', $sbResponseBody);
if (!$success) {
    print $http->LastErrorText, "\n";
    exit;
}

my $jResp = Chilkat2::JsonObject->new;
$jResp->LoadSb($sbResponseBody);
$jResp->EmitCompact(0);

print "Response Body:\n";
print $jResp->Emit(), "\n";

my $respStatusCode = $http->LastStatus;
print 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
    print "Response Header:\n";
    print $http->LastHeader, "\n";
    print "Failed.\n";
    exit;
}

# 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

my $id = '';
my $first_name = '';
my $last_name = '';
my $email = '';
my $v_type = 0;
my $pmi = 0;
my $timezone = '';
my $verified = 0;
my $created_at = '';
my $last_login_time = '';
my $language = '';
my $phone_number = '';
my $status = '';
my $role_id = '';

my $page_count = $jResp->IntOf('page_count');
my $page_number = $jResp->IntOf('page_number');
my $page_size = $jResp->IntOf('page_size');
my $total_records = $jResp->IntOf('total_records');
my $i = 0;
my $count_i = $jResp->SizeOfArray('users');
while ($i < $count_i) {
    $jResp->I($i);
    $id = $jResp->StringOf('users[i].id');
    $first_name = $jResp->StringOf('users[i].first_name');
    $last_name = $jResp->StringOf('users[i].last_name');
    $email = $jResp->StringOf('users[i].email');
    $v_type = $jResp->IntOf('users[i].type');
    $pmi = $jResp->IntOf('users[i].pmi');
    $timezone = $jResp->StringOf('users[i].timezone');
    $verified = $jResp->IntOf('users[i].verified');
    $created_at = $jResp->StringOf('users[i].created_at');
    $last_login_time = $jResp->StringOf('users[i].last_login_time');
    $language = $jResp->StringOf('users[i].language');
    $phone_number = $jResp->StringOf('users[i].phone_number');
    $status = $jResp->StringOf('users[i].status');
    $role_id = $jResp->StringOf('users[i].role_id');
    $i = $i + 1;
}