C# JWT token invalid

I am having an issue with token, the error is invalid token.
Please help.

string key = "MY_Key";
             

                var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
                var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
                var header = new JwtHeader(credentials);
                var payload = new JwtPayload
                   {
                        { "iss", "my_API"},
                      { "iat", DateTimeOffset.Now.ToUnixTimeSeconds() },
                       { "exp", DateTimeOffset.Now.ToUnixTimeSeconds() + 1400 },
                   };
                        
                var secToken = new JwtSecurityToken(header, payload);
                var handler = new JwtSecurityTokenHandler();

                var tokenString = handler.WriteToken(secToken);

                Console.WriteLine(tokenString);
               
                var token = handler.ReadJwtToken(tokenString);

                Console.WriteLine(token.Payload.First().Value);
          
                RestClient client = new RestClient();
                client.BaseUrl = new Uri("https://api.zoom.us/v2");
           
                RestRequest request = new RestRequest("/users", Method.GET);
                request.AddHeader("Authorization", "Bearer " + tokenString.ToString());
               
                request.AddHeader("User-Agent", "Zoom-api-Jwt-Request");
                request.AddHeader("Content-Type", "application/json");
                var response = client.Execute(request);
                Console.Write(response.StatusCode);

Hi @nvanderson,

Welcome to the Zoom Developer Forum. We are happy to help you.

  • If you are using the windows SDK, then there are two types of Authentications that you need to do within your implementation:
  1. SDK Authentication
  2. JWT Authentication

To do the SDK Authentication, you would need the SDK API key and secret. To get the SDK key and secret, please visit https://marketplace.zoom.us/docs/sdk/native-sdks/preface/sdk-keys-secrets. The SDK authentication is necessary for Zoom to allow you to use our SDKs.

JWT Authentication: If you need to do JWT authentication, please refer to the example mentioned here: https://github.com/zoom/zoom-sdk-windows/blob/45f58efe46cab9af80e2d9a7f24a1893e75a34c9/demo/sdk_demo_v2/zoomHmacSHA256.cpp. Even though this code is not written in C#, it is based on the same logic.
The JWT token enables you to use our APIs. To find a list of all our APIs, please visit: https://marketplace.zoom.us/docs/api-reference/introduction

I hope this helps.

Thanks

Yes I followed the links, unfortunately they did not solve my problem.

@Michael_Purnell can you take a look here?

Hey @nvanderson,

Can you try hard coding the JWT token from your Zoom App Dashboard and see if the error goes away?

request.AddHeader("Authorization", "Bearer JWTTOKEN");

If it works, that would mean your JWT generation code is not working properly.

Thanks,
Tommy

Yes, the hard-coded value works. I am trying to figure our what is wrong the token generation…?!:thinking:

Hey @nvanderson,

I am no expert in C#. You can try using one of the .net libraries here to generate the JWT.

Also check out this stack overflow question about how to generate a JWT in C#

Thanks,
Tommy

Finally figure it out…:laughing:

1 Like

Happy to hear @nvanderson :slight_smile:

If you don’t mind, could you post the solution so if others have trouble they can see how you fixed it?

Thanks,
Tommy

According to this https://marketplace.zoom.us/docs/guides/authorization/jwt/jwt-with-zoom
only 2 parameters are needed inside the playload.
{ “iss”: “API_KEY”,
“exp”: 1496091964000
}

However, 4 are needed
string s =null;
like this { aud = s, iss = Key, exp = secondsSinceEpoch, iat= NowsecondsSinceEpoch };

Thanks for posting this @nvanderson!