C# IRestRespose Fails - JWT

I am running the following piece of code for a test. I get back a valid token. I tested it at JWT…
The response returns a response.IsSuccessful as false and no content. I expected a list of users…

I put my API key and Secret Key in where it is commented.

Thanks for any help

     var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler();
        var now = DateTime.UtcNow;
        var apiSecret =  "<your API secret here>";
        byte[] symmetricKey = Encoding.ASCII.GetBytes(apiSecret);

        var tokenDescriptor = new SecurityTokenDescriptor
        {
            Issuer = "<your API key here>",
            Expires = now.AddSeconds(30),
            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256),
        };
        var token = tokenHandler.CreateToken(tokenDescriptor);

        var tokenString = tokenHandler.WriteToken(token);

        var client = new RestClient("https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1");
        var request = new RestRequest(Method.GET);
        request.AddHeader("content-type", "application/json");
        request.AddHeader("authorization", String.Format("Bearer {0}", tokenString));

        IRestResponse response = client.Execute(request);

        MessageBox.Show(response.IsSuccessful.ToString());

Hey @jim.hubbard,

Can you please share the response JSON?

Thanks,
Tommy

I am not getting any response. Null Response.
error: InnerException = {“Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.”}

I tried another piece of code and got the same result.

   private void GetAccountUsers(string ZoomApiKey, string ZoomApiSecret)
    {
        try
        {

            ZoomToken zt = new ZoomToken();
            string Token = zt.GetToken(ZoomApiKey,ZoomApiSecret);

            //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

//WHEN IT GETS HERE I GET AN ERROR. THE RESPONSE IN NULL.
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);
            }
        }
    }

I tried using Curl and got an invalid token. I copied the toke from my app I made it valid for a day.

$ Curl -X POST -H “Accept:application/json” -H “Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImlzcyI6Im1JU2pDVGFCUVh1aDhRb20yNEZabHciLCJleHAiOjE1OTQ0OTE5MTIsImlhdCI6MTU5NDQwNTUxMn0.GLt8txVLUc2LpFR9Da2Lk3tzppX3lI_ejcVq0A7VE70” https://api.zoom.us/v2/users?status=active&page_size=300&page_number=1
[2] 603
[3] 604
[1] Done Curl -X POST -H “Accept:application/json” -H “Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOm51bGwsImlzcyI6Im1JU2pDVGFCUVh1aDhRb20yNEZabHciLCJleHAiOjE1OTQzNzk2ODAsImlhdCI6MTU5NDM3OTM4MH0.FMNM1sqC3XgEm4PnmvEoTx0yUgVdfUUAGpnuXcj1XlY” https://api.zoom.us/v2/users?status=active
[3]+ Done page_size=300

jim.hubbard@UIH001900 MINGW64 /
$ % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 46 0 46 0 0 81 0 --:–:-- --:–:-- --:–:-- 81{“code”:124,“message”:“Invalid access token.”}

I am going to regenerate the secret key and try again…

code: 124
message: “Invalid access token.”

Hey @jim.hubbard,

When you are setting your JWT Authorization header, you are missing the bearer keyword.

"Authorization": "Bearer JWT_TOKEN_HERE"

Thanks,
Tommy

Hi Tommy,

I finally got this working. I regenerated my JWT secret and that fixed the problem. I am working on some of the functionality now…

Thanks for getting back to me.

J

1 Like

Happy to hear you got it working! :slight_smile:

Thanks,
Tommy