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.