SQL Server
SQL Server
Zoom API - Create JWT to Authenticate API Requests
See more Zoom Examples
Creates a JWT for the Zoom API.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- 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...
DECLARE @apiKey nvarchar(4000)
SELECT @apiKey = 'o9rw6Gq0RnqlkfaSqtCMOA'
DECLARE @apiSecret nvarchar(4000)
SELECT @apiSecret = 'UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR'
-- Create a JWT to authenticate Zoom API requests.
DECLARE @jwt int
EXEC @hr = sp_OACreate 'Chilkat.Jwt', @jwt OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @jose int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jose OUT
EXEC sp_OAMethod @jose, 'UpdateString', @success OUT, 'alg', 'HS256'
EXEC sp_OAMethod @jose, 'UpdateString', @success OUT, 'typ', 'JWT'
-- Build claims to look like this:
-- {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
DECLARE @claims int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @claims OUT
EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'iss', @apiKey
EXEC sp_OAMethod @claims, 'UpdateNull', @success OUT, 'aud'
-- Set the timestamp of when the JWT was created to now.
DECLARE @curDateTime int
EXEC sp_OAMethod @jwt, 'GenNumericDate', @curDateTime OUT, 0
EXEC sp_OAMethod @claims, 'AddIntAt', @success OUT, -1, 'iat', @curDateTime
-- Set the timestamp defining an expiration time (end time) for the token
-- to be now + 1 month(3600 * 24 * 30 seconds)
DECLARE @oneMonth int
SELECT @oneMonth = 3600 * 24 * 30
EXEC sp_OAMethod @claims, 'AddIntAt', @success OUT, -1, 'exp', @curDateTime + @oneMonth
-- Produce the smallest possible JWT:
EXEC sp_OASetProperty @jwt, 'AutoCompact', 1
DECLARE @strJwt nvarchar(4000)
EXEC sp_OAMethod @jose, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @claims, 'Emit', @sTmp1 OUT
EXEC sp_OAMethod @jwt, 'CreateJwt', @strJwt OUT, @sTmp0, @sTmp1, @apiSecret
PRINT @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
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
-- 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
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
-- Adds the "Authorization: Bearer { your_token }" header.
EXEC sp_OASetProperty @http, 'AuthToken', @strJwt
DECLARE @sbResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1', @sbResponseBody
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @jose
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
RETURN
END
DECLARE @jResp int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @jose
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
-- 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
DECLARE @id nvarchar(4000)
DECLARE @first_name nvarchar(4000)
DECLARE @last_name nvarchar(4000)
DECLARE @email nvarchar(4000)
DECLARE @v_type int
DECLARE @pmi int
DECLARE @timezone nvarchar(4000)
DECLARE @verified int
DECLARE @created_at nvarchar(4000)
DECLARE @last_login_time nvarchar(4000)
DECLARE @language nvarchar(4000)
DECLARE @phone_number nvarchar(4000)
DECLARE @status nvarchar(4000)
DECLARE @role_id nvarchar(4000)
DECLARE @page_count int
EXEC sp_OAMethod @jResp, 'IntOf', @page_count OUT, 'page_count'
DECLARE @page_number int
EXEC sp_OAMethod @jResp, 'IntOf', @page_number OUT, 'page_number'
DECLARE @page_size int
EXEC sp_OAMethod @jResp, 'IntOf', @page_size OUT, 'page_size'
DECLARE @total_records int
EXEC sp_OAMethod @jResp, 'IntOf', @total_records OUT, 'total_records'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'users'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'users[i].id'
EXEC sp_OAMethod @jResp, 'StringOf', @first_name OUT, 'users[i].first_name'
EXEC sp_OAMethod @jResp, 'StringOf', @last_name OUT, 'users[i].last_name'
EXEC sp_OAMethod @jResp, 'StringOf', @email OUT, 'users[i].email'
EXEC sp_OAMethod @jResp, 'IntOf', @v_type OUT, 'users[i].type'
EXEC sp_OAMethod @jResp, 'IntOf', @pmi OUT, 'users[i].pmi'
EXEC sp_OAMethod @jResp, 'StringOf', @timezone OUT, 'users[i].timezone'
EXEC sp_OAMethod @jResp, 'IntOf', @verified OUT, 'users[i].verified'
EXEC sp_OAMethod @jResp, 'StringOf', @created_at OUT, 'users[i].created_at'
EXEC sp_OAMethod @jResp, 'StringOf', @last_login_time OUT, 'users[i].last_login_time'
EXEC sp_OAMethod @jResp, 'StringOf', @language OUT, 'users[i].language'
EXEC sp_OAMethod @jResp, 'StringOf', @phone_number OUT, 'users[i].phone_number'
EXEC sp_OAMethod @jResp, 'StringOf', @status OUT, 'users[i].status'
EXEC sp_OAMethod @jResp, 'StringOf', @role_id OUT, 'users[i].role_id'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @jose
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO