How to create JWT token using REST api in c#

Make sure you are using correct API secret and Key, and your account supports API calls (i.e. not a free one, it may’ve changed lately though)
LogIn Here (https://marketplace.zoom.us/user/build)
Make sure your app is JWT and its active


Here is the class that generates my token

using System;
using System.Text;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;

namespace ZoomIntegration.Configuration {
    
    public class ZoomToken
    {
        public ZoomToken(string ZoomApiKey, string ZoomApiSecret )
        {   
            DateTime Expiry = DateTime.UtcNow.AddMinutes(5);
            string ApiKey = ZoomApiKey;
            string ApiSecret = ZoomApiSecret;

            int ts = (int)(Expiry - new DateTime(1970, 1, 1)).TotalSeconds;

            // Create Security key  using private key above:
            // note that latest version of JWT using Microsoft namespace instead of System
            var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(ApiSecret));

            // Also note that securityKey length should be >256b
            // so you have to make sure that your private key has a proper length
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            //Finally create a Token
            var header = new JwtHeader(credentials);

            //Zoom Required Payload
            var payload = new JwtPayload
            {
                { "iss", ApiKey},
                { "exp", ts },
            };

            var secToken = new JwtSecurityToken(header, payload);
            var handler = new JwtSecurityTokenHandler();

            // Token to String so you can use it in your client
            var tokenString = handler.WriteToken(secToken);
            //string Token = tokenString;
            this.Token = tokenString;
        }
        
        public string Token { get; set; }
    }
    
}
//Here is the method to get Zoom users
private void GetAccountUsers(string ZoomApiKey, string ZoomApiSecret) {
            try
            {
                
                ZoomToken zt = new ZoomToken(ZoomApiKey, ZoomApiSecret);
                string Token = zt.Token;
                
                //Create new Request
                string BaseUrl = "https://api.zoom.us/v2/users?status=active&page_size=300&page_number=1";
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(BaseUrl);
                myHttpWebRequest.Method = "GET";
                myHttpWebRequest.ContentType = "application/json;";
                myHttpWebRequest.Accept = "application/json;";
                myHttpWebRequest.Headers.Add("Authorization", String.Format("Bearer {0}", Token));

                //Get the associated response for the above request
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                using (StreamReader MyStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream(), true))
                {
                    this.AccountUsers = JsonConvert.DeserializeObject<AccountUsers>(MyStreamReader.ReadToEnd());
                }
                
                myHttpWebResponse.Close();
                myHttpWebResponse.Dispose();
        
            }
            catch (WebException ex)
            {
                int errorCode = (int)((HttpWebResponse)ex.Response).StatusCode;
                if (errorCode != 0)
                {
                    ErrorCode = errorCode;
                    Stream MyStream = ex.Response.GetResponseStream();
                    StreamReader MyStreamReader = new StreamReader(MyStream, true);
                    this.ZoomException = JsonConvert.DeserializeObject<ZoomErrorResponse>(MyStreamReader.ReadToEnd());                  

                    //would be nice to check ZoomException for Null before throwing
                    throw new Exception(this.ZoomException.Message);
                }
            }
        }

//Here are 3 models for the response
[JsonObject]
    public class AccountUsers{
        
        [JsonProperty("page_count")]
        public Int32 PageCount { get; set; }

        [JsonProperty("page_number")]
        public Int32 PageNumber { get; set; }
        
        [JsonProperty("page_size")]
        public Int32 PageSize { get; set; }
        
        [JsonProperty("total_records")]
        public Int32 TotalRecords { get; set; }
        
        [JsonProperty("users")]
        public List<User> Users { get; set;}
        
        
    }
    
    [JsonObject]
    public class User{
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("first_name")]
        public string FirstName { get; set; }

        [JsonProperty("last_name")]
        public string LastName { get; set; }
        
        [JsonProperty("email")]
        public string Email { get; set; }

        [JsonProperty("type")]
        public Int32 Type { get; set; }

        [JsonProperty("pmi")]
        public string Pmi { get; set; }

        [JsonProperty("timezone")]
        public string TimeZone { get; set; }

        [JsonProperty("verified")]
        public Int32 Verified { get; set; }
        
        [JsonProperty("dept")]
        public string Department { get; set; }
        
        [JsonProperty("created_at")]
        public DateTime CreatedAt { get; set; }
        
        [JsonProperty("last_login_time")]
        public DateTime LastLoginTime { get; set; }
        
        [JsonProperty("last_client_version")]
        public string LastClientVersion { get; set; }
        
    }
    
    [JsonObject]
    public class ZoomErrorResponse {
    
        [JsonProperty("code")]
        public String Code { get; set; }
        
        [JsonProperty("message")]
        public String Message { get; set; }
    }

5 Likes