Jira Connect Oauth Token always return token expired

I’m trying to write an Add-on using c#, I’ve been able to retrieve oauth id, share secret and client id. But, when I’m trying to get oauth-access-token, atlassian connect always return my token has been expired.

This is my code to generate JWT token:

private double ConvertToUnixTimestamp(DateTime date)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            TimeSpan diff = date.ToUniversalTime() - origin;
            return Math.Floor(diff.TotalSeconds);
        }

public string GenerateJwtToken(string instanceBaseUrl, string userKey, string clientId, string secret)
        {
            var now = ConvertToUnixTimestamp(DateTime.UtcNow);
            var exp = now + EXPIRY_SECONDS;
            var payloads = new Dictionary<string, object>
                {
                    { "iss", "urn:atlassian:connect:clientid:" + clientId  },
                    { "sub", "urn:atlassian:connect:userkey:" + userKey  },
                    { "tnt", instanceBaseUrl  },
                    { "aud", AUTHORIZATION_SERVER_URL  },
                    { "iat", now  },
                    { "exp", exp  }
                };

            return JsonWebToken.Encode(payloads, secret, JwtHashAlgorithm.HS256);
        }

Any help is very appreciated. Thanks.

I believe the iat and exp values are expressed in terms of (integer) number of seconds from the start of the epoch. I think your now and exp values will be double values, right?

Yeah, it is double. I have tried to change to int or long type of value but still get the same error.
Here is the value of those variables. now: 1494288801, exp: 1494288811.

Could it be that you are missing the qsh parameter? See https://developer.atlassian.com/static/connect/docs/latest/concepts/understanding-jwt.html.

Ah, I’m able to fix it by changing my way to get the TotalSecond value…
I’m using this:
TimeSpan.FromTicks(DateTime.Now.Ticks).TotalSeconds;

All works fine now… Thanks for the help…