Sample code for 30+ languages & platforms
PureBasic

Zoom API - Create JWT to Authenticate API Requests

See more Zoom Examples

Creates a JWT for the Zoom API.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkJwt.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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...
    apiKey.s = "o9rw6Gq0RnqlkfaSqtCMOA"
    apiSecret.s = "UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR"

    ; Create a JWT to authenticate Zoom API requests.
    jwt.i = CkJwt::ckCreate()
    If jwt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jose.i = CkJsonObject::ckCreate()
    If jose.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckUpdateString(jose,"alg","HS256")
    success = CkJsonObject::ckUpdateString(jose,"typ","JWT")

    ; Build claims to look like this:
    ; {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
    claims.i = CkJsonObject::ckCreate()
    If claims.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckUpdateString(claims,"iss",apiKey)
    success = CkJsonObject::ckUpdateNull(claims,"aud")

    ; Set the timestamp of when the JWT was created to now.
    curDateTime.i = CkJwt::ckGenNumericDate(jwt,0)
    success = CkJsonObject::ckAddIntAt(claims,-1,"iat",curDateTime)

    ; Set the timestamp defining an expiration time (end time) for the token
    ; to be now + 1 month(3600 * 24 * 30 seconds)
    oneMonth.i = 3600 * 24 * 30
    success = CkJsonObject::ckAddIntAt(claims,-1,"exp",curDateTime + oneMonth)

    ; Produce the smallest possible JWT:
    CkJwt::setCkAutoCompact(jwt, 1)

    strJwt.s = CkJwt::ckCreateJwt(jwt,CkJsonObject::ckEmit(jose),CkJsonObject::ckEmit(claims),apiSecret)

    Debug 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.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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

    CkHttp::ckSetRequestHeader(http,"content-type","application/json")
    ; Adds the "Authorization: Bearer { your_token }" header.
    CkHttp::setCkAuthToken(http, strJwt)

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckQuickGetSb(http,"https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkJwt::ckDispose(jwt)
        CkJsonObject::ckDispose(jose)
        CkJsonObject::ckDispose(claims)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttp::ckLastHeader(http)
        Debug "Failed."
        CkJwt::ckDispose(jwt)
        CkJsonObject::ckDispose(jose)
        CkJsonObject::ckDispose(claims)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    ; 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

    id.s
    first_name.s
    last_name.s
    email.s
    v_type.i
    pmi.i
    timezone.s
    verified.i
    created_at.s
    last_login_time.s
    language.s
    phone_number.s
    status.s
    role_id.s

    page_count.i = CkJsonObject::ckIntOf(jResp,"page_count")
    page_number.i = CkJsonObject::ckIntOf(jResp,"page_number")
    page_size.i = CkJsonObject::ckIntOf(jResp,"page_size")
    total_records.i = CkJsonObject::ckIntOf(jResp,"total_records")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"users")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        id = CkJsonObject::ckStringOf(jResp,"users[i].id")
        first_name = CkJsonObject::ckStringOf(jResp,"users[i].first_name")
        last_name = CkJsonObject::ckStringOf(jResp,"users[i].last_name")
        email = CkJsonObject::ckStringOf(jResp,"users[i].email")
        v_type = CkJsonObject::ckIntOf(jResp,"users[i].type")
        pmi = CkJsonObject::ckIntOf(jResp,"users[i].pmi")
        timezone = CkJsonObject::ckStringOf(jResp,"users[i].timezone")
        verified = CkJsonObject::ckIntOf(jResp,"users[i].verified")
        created_at = CkJsonObject::ckStringOf(jResp,"users[i].created_at")
        last_login_time = CkJsonObject::ckStringOf(jResp,"users[i].last_login_time")
        language = CkJsonObject::ckStringOf(jResp,"users[i].language")
        phone_number = CkJsonObject::ckStringOf(jResp,"users[i].phone_number")
        status = CkJsonObject::ckStringOf(jResp,"users[i].status")
        role_id = CkJsonObject::ckStringOf(jResp,"users[i].role_id")
        i = i + 1
    Wend


    CkJwt::ckDispose(jwt)
    CkJsonObject::ckDispose(jose)
    CkJsonObject::ckDispose(claims)
    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure