Swift
Swift
Zoom API - Create JWT to Authenticate API Requests
See more Zoom Examples
Creates a JWT for the Zoom API.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = 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...
var apiKey: String? = "o9rw6Gq0RnqlkfaSqtCMOA"
var apiSecret: String? = "UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR"
// Create a JWT to authenticate Zoom API requests.
let jwt = CkoJwt()!
let jose = CkoJsonObject()!
success = jose.updateString(jsonPath: "alg", value: "HS256")
success = jose.updateString(jsonPath: "typ", value: "JWT")
// Build claims to look like this:
// {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
let claims = CkoJsonObject()!
success = claims.updateString(jsonPath: "iss", value: apiKey)
success = claims.updateNull(jsonPath: "aud")
// Set the timestamp of when the JWT was created to now.
var curDateTime: Int = jwt.genNumericDate(numSecOffset: 0).intValue
success = claims.addInt(at: -1, name: "iat", value: curDateTime)
// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 month(3600 * 24 * 30 seconds)
var oneMonth: Int = 3600 * 24 * 30
success = claims.addInt(at: -1, name: "exp", value: curDateTime + oneMonth)
// Produce the smallest possible JWT:
jwt.autoCompact = true
var strJwt: String? = jwt.create(header: jose.emit(), payload: claims.emit(), password: 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
let http = CkoHttp()!
// 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(name: "content-type", value: "application/json")
// Adds the "Authorization: Bearer { your_token }" header.
http.authToken = strJwt
let sbResponseBody = CkoStringBuilder()!
success = http.quickGetSb(url: "https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1", sbContent: sbResponseBody)
if success == false {
print("\(http.lastErrorText!)")
return
}
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = http.lastStatus.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(http.lastHeader!)")
print("Failed.")
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
var id: String?
var first_name: String?
var last_name: String?
var email: String?
var v_type: Int
var pmi: Int
var timezone: String?
var verified: Int
var created_at: String?
var last_login_time: String?
var language: String?
var phone_number: String?
var status: String?
var role_id: String?
var page_count: Int = jResp.int(of: "page_count").intValue
var page_number: Int = jResp.int(of: "page_number").intValue
var page_size: Int = jResp.int(of: "page_size").intValue
var total_records: Int = jResp.int(of: "total_records").intValue
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "users").intValue
while i < count_i {
jResp.i = i
id = jResp.string(of: "users[i].id")
first_name = jResp.string(of: "users[i].first_name")
last_name = jResp.string(of: "users[i].last_name")
email = jResp.string(of: "users[i].email")
v_type = jResp.int(of: "users[i].type").intValue
pmi = jResp.int(of: "users[i].pmi").intValue
timezone = jResp.string(of: "users[i].timezone")
verified = jResp.int(of: "users[i].verified").intValue
created_at = jResp.string(of: "users[i].created_at")
last_login_time = jResp.string(of: "users[i].last_login_time")
language = jResp.string(of: "users[i].language")
phone_number = jResp.string(of: "users[i].phone_number")
status = jResp.string(of: "users[i].status")
role_id = jResp.string(of: "users[i].role_id")
i = i + 1
}
}