How to create JWT token using REST api in c#

Thanks @k.krylov for the code. You saved my day. It has fixed now.
The issue was with this line:

var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

I have updated line to below one and it worked. Thanks a ton.
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

I think when I added that line it was taking a reference from some other DLL and hence it was asking to add HmacSha256Signature algoritham.

Again, thank you!

1 Like

Happy to hear your issue is fixed @kshitij.gugale! :slight_smile:

Thanks for your help @k.krylov! :slight_smile:

-Tommy

Hi team,
In my web application , I am calling zoom create api using jwt token which is generated by using api_key and secret key which giving me response as expected on local server (development environment).But same on production giving me “” blank response.

What to do in order to get proper response on production.

Same apikey and secret key are used to generate jwt token on production.
Thanks
Trupti

Hello,

I have used this code. But i Am getting an error of assembly files…

hi can u please tell me for generating jwt token the api key and secret key u are using is of which type of account?
As i m using basic account type and its free account
i am getting meeting response when api call locally but not getting any response not even error when try to calling from different server where i published my code.

Thank You, Thank You, Thank You, @k.krylov. Your Zoom Token Class worked perfectly. I made sure to give you credit in our source code. For better of worse, the only thing I added was a “Timeout” parameter to help us manage token expiration in an administrative module setting on our public Zoom meeting registration control. Here’s an image of just the slight modification. Thanks Again… Your class saved me tons of time.

1 Like

Happy to hear you got it working @mike.gold! :slight_smile:

-Tommy

Hey @harshit.singhal,

Please share steps to reproduce the issue.

Thanks,
Tommy

Hey @trupti.patlekar,

Please share steps to reproduce your issue.

Thanks,
Tommy

Hey,

Please find the below code for your reference.

public string GenerateToken ()
{
var tokenHandler = new JwtSecurityTokenHandler();
var now = DateTime.UtcNow;
var apiSecret = “”;
byte symmetricKey = Encoding.ASCII.GetBytes(apiSecret);

var tokenDescriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor
{
Issuer = “”,
Expires = now.AddSeconds(30),
SigningCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(symmetricKey), Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256),
};
var token = tokenHandler.CreateToken(tokenDescriptor);

var tokenString = tokenHandler.WriteToken(token);
}

I think the problem is with dll. Can you tell me the version for dll

1 Like

thank you . I got route cause of my issue which is the server where i am executing API don’t have zoom access.

Now the server where I am calling zoom api for create meeting still providing response as blank.
i am using c# asp.net core application where i have implemented function as below. In that function jwt token geerated using ZoomToken() function.
This is working fine in my local server but giving blank response when run on UAT server whish i used to host application.
Kindly find the following steps:
this function i am calling from our iis server
var client = new RestClient(“https://api.zoom.us/v2/users/me/meetings”);
var request = new RestRequest(Method.POST);
request.AddHeader(“content-type”, “application/json”);
request.AddHeader(“authorization”, String.Format(“Bearer {0}”,ZoomUtility.ZoomToken()));
request.AddParameter(“application/json”, “{“topic”:“Meeting”,“type”:“1”,“start_time”:”" + sdate + “”,“duration”:“120”,“schedule_for”:null,“timezone”:“Asia/Calcutta”,“password”:"",“agenda”:"",“recurrence”:{“type”:“2”,“repeat_interval”:"",“weekly_days”:"",“monthly_day”:"",“monthly_week”:"",“monthly_week_day”:"",“end_times”:"",“end_date_time”:"" + edate + “”},“settings”:{“host_video”:true,“participant_video”:false,“cn_meeting”:false,“in_meeting”:true,“join_before_host”:false,“mute_upon_entry”:false,“watermark”:false,“use_pmi”:false,“approval_type”:“1”,“registration_type”:“1”,“audio”:"",“auto_recording”:"",“enforce_login”:"",“enforce_login_domains”:"",“alternative_hosts”:"",“global_dial_in_countries”:[""],“registrants_email_notification”:true}}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var content = response.Content;

public static string ZoomToken()
{
// Token will be good for 20 minutes
DateTime Expiry = DateTime.UtcNow.AddMinutes(1440);

        int ts = (int)(Expiry - new DateTime(1970, 1, 1)).TotalSeconds;
        string apiSecret = ZoomAPISecretString();
        string apiKey = ZoomAppKeyString();
        // Create Security key using private key above:
        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(apiSecret));

        // I did changes in below line because DLL needed HmacSha256Signature instead of HmacSha256
        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);

        return tokenString;
    }

Hi , My issue has been resolved by my network team .it was access right issue.

1 Like

Happy to hear you got it sorted out! :slight_smile:

Thanks,
Tommy

Thanks for your suggestions @harshit.singhal! :slight_smile:

-Tommy

Hey,

Can you help me out in this issue? Please send me the resolution asap.

Hey @harshit.singhal,

Please see the JWT docs and Libraries here:

Thanks,
Tommy

Hey Tommy,

Thanks a lot to you. My Problem is resolved.

1 Like

Happy to hear you got the issue sorted out! :slight_smile:

Thanks,
Tommy

@k.krylov @mike.gold Both of your codes helped a lot. You saved spending a ton of time looking into the docs.

Tommy Gaessler, could you please update the docs with the sample code to get JWT tokens? It will save a lot of time for new developers.