How to create JWT token using REST api in c#

Glad they were able to help, @puneeth!

I will share this feedback with our team, to add more samples like this.

Best,
Will

Hi,

I am using the same code as yours. I had to make a few changes as we get the request related details from the DB. It worked perfectly in our dev and staging platform.

We deployed it into production and used the production API key and secret. The token generates successfully, and the expiry date also is as expected. However, when we make a request using the token, it throws "Content":"{\"code\":124,\"message\":\"Access token is expired.\"}

Below is my code for reference. Could you please let me know what is going wrong?

using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;

namespace Library.Zoom
{
    /// <summary>
    /// https://devforum.zoom.us/t/how-to-create-jwt-token-using-rest-api-in-c/6620/20?u=puneeth
    /// </summary>
    public class APIToken
    {
        private static string token;

        private static DateTime expiryDate;

        public static string GetToken()
        {
            if (!string.IsNullOrEmpty(token)
                && DateTime.Compare(DateTime.UtcNow, expiryDate) < 0)
            {
                return token;
            }
            else
            {
                try
                {
                    //Default expiry of the token in seconds
                    string expiresIn = "3600";

                    //Get api_key, api_secret, expires_in from DB

					DateTime expiresOn = DateTime.UtcNow.AddSeconds(Convert.ToDouble(expiresIn));

					long ts = (long)(expiresOn - 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
					SymmetricSecurityKey securityKey = new 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
					SigningCredentials credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

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

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

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

					//Token to String so you can use it in your client
					token = handler.WriteToken(secToken);
					expiryDate = secToken.ValidTo;
                }
                catch (Exception ex)
                {
                    ins_error_log log = new ins_error_log();
                    log.runSP(ex, false);
                }
            }

            return token;
        }
    }
}
1 Like

I had similar issue on my prod server. Turned out time was out of sync. Make sure local time on your prod box is in sync, second option is to increase expiry time in minutes, bump it up to some larger value, I did not see any restrictions in the documentation.

Thanks for sharing, @k.krylov!

I need your help, I am trying to integrate zoom to my ASP.Net Core project. After generating the token what should I do? I am really stuck at this point. I would be grateful if you could help.

Hi @engiibahaa,

Once youā€™ve generated a JWT token, you can authenticate requests to any of our API endpoints. You can also utilize our Web SDK via JWT authentication:

If you have specific questions about either of these capabilities, just let us know.

Thanks,
Will

I have written a library to make working with Zoomā€™s Java Web Tokens (JWTs) in C# much easier:
https://github.com/apresence/ZoomJWT

Enjoy!

Thanks for sharing, @apresence! Iā€™m sure our community will find this helpful. :slight_smile: