Sample code for 30+ languages & platforms
C

Zoom API - Create JWT to Authenticate API Requests

See more Zoom Examples

Creates a JWT for the Zoom API.

Chilkat C Downloads

C
#include <C_CkJwt.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *apiKey;
    const char *apiSecret;
    HCkJwt jwt;
    HCkJsonObject jose;
    HCkJsonObject claims;
    int curDateTime;
    int oneMonth;
    const char *strJwt;
    HCkHttp http;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *id;
    const char *first_name;
    const char *last_name;
    const char *email;
    int v_type;
    int pmi;
    const char *timezone;
    int verified;
    const char *created_at;
    const char *last_login_time;
    const char *language;
    const char *phone_number;
    const char *status;
    const char *role_id;
    int page_count;
    int page_number;
    int page_size;
    int total_records;
    int i;
    int count_i;

    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...
    apiKey = "o9rw6Gq0RnqlkfaSqtCMOA";
    apiSecret = "UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR";

    // Create a JWT to authenticate Zoom API requests.
    jwt = CkJwt_Create();

    jose = CkJsonObject_Create();
    success = CkJsonObject_UpdateString(jose,"alg","HS256");
    success = CkJsonObject_UpdateString(jose,"typ","JWT");

    // Build claims to look like this:
    // {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
    claims = CkJsonObject_Create();
    success = CkJsonObject_UpdateString(claims,"iss",apiKey);
    success = CkJsonObject_UpdateNull(claims,"aud");

    // Set the timestamp of when the JWT was created to now.
    curDateTime = CkJwt_GenNumericDate(jwt,0);
    success = CkJsonObject_AddIntAt(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 = 3600 * 24 * 30;
    success = CkJsonObject_AddIntAt(claims,-1,"exp",curDateTime + oneMonth);

    // Produce the smallest possible JWT:
    CkJwt_putAutoCompact(jwt,TRUE);

    strJwt = CkJwt_createJwt(jwt,CkJsonObject_emit(jose),CkJsonObject_emit(claims),apiSecret);

    printf("%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

    http = CkHttp_Create();

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

    sbResponseBody = CkStringBuilder_Create();
    success = CkHttp_QuickGetSb(http,"https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1",sbResponseBody);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkJwt_Dispose(jwt);
        CkJsonObject_Dispose(jose);
        CkJsonObject_Dispose(claims);
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttp_getLastStatus(http);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttp_lastHeader(http));
        printf("Failed.\n");
        CkJwt_Dispose(jwt);
        CkJsonObject_Dispose(jose);
        CkJsonObject_Dispose(claims);
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        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

    page_count = CkJsonObject_IntOf(jResp,"page_count");
    page_number = CkJsonObject_IntOf(jResp,"page_number");
    page_size = CkJsonObject_IntOf(jResp,"page_size");
    total_records = CkJsonObject_IntOf(jResp,"total_records");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"users");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        id = CkJsonObject_stringOf(jResp,"users[i].id");
        first_name = CkJsonObject_stringOf(jResp,"users[i].first_name");
        last_name = CkJsonObject_stringOf(jResp,"users[i].last_name");
        email = CkJsonObject_stringOf(jResp,"users[i].email");
        v_type = CkJsonObject_IntOf(jResp,"users[i].type");
        pmi = CkJsonObject_IntOf(jResp,"users[i].pmi");
        timezone = CkJsonObject_stringOf(jResp,"users[i].timezone");
        verified = CkJsonObject_IntOf(jResp,"users[i].verified");
        created_at = CkJsonObject_stringOf(jResp,"users[i].created_at");
        last_login_time = CkJsonObject_stringOf(jResp,"users[i].last_login_time");
        language = CkJsonObject_stringOf(jResp,"users[i].language");
        phone_number = CkJsonObject_stringOf(jResp,"users[i].phone_number");
        status = CkJsonObject_stringOf(jResp,"users[i].status");
        role_id = CkJsonObject_stringOf(jResp,"users[i].role_id");
        i = i + 1;
    }



    CkJwt_Dispose(jwt);
    CkJsonObject_Dispose(jose);
    CkJsonObject_Dispose(claims);
    CkHttp_Dispose(http);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }