Success. I had to put the exp like 2 days in the future and now I can perhaps send messages.
Here is the working C# JWT generation code.
public static long ToUnixTime(DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalSeconds);
}
private string GetSignedJWT()
{
DateTime now = DateTime.Now;
DateTime expires = now + TimeSpan.FromDays(2);
long exp = ToUnixTime(expires);
var claimsIdentity = new ClaimsIdentity(new List<Claim>()
{
new Claim(ClaimTypes.NameIdentifier, _mUserId),
new Claim(ClaimTypes.Role, "external"),
}, "Custom");
string str = VALUE_BACKEND_SECRET;
int mod4 = str.Length % 4;
if (mod4 > 0)
{
str += new string('=', 4 - mod4);
}
byte[] keyBytes = Convert.FromBase64String(str);
var signingKey =
new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(keyBytes);
var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(
signingKey,
SecurityAlgorithms.HmacSha256Signature);
var securityTokenDescriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor()
{
Issuer = "Twitch",
Subject = claimsIdentity,
Audience = "OAuth2",
Expires = expires,
NotBefore = now,
IssuedAt = now,
SigningCredentials = signingCredentials
};
var jwtHeader = new JwtHeader(signingCredentials);
JObject jsonPayload = new JObject();
jsonPayload.Add("exp", exp);
jsonPayload.Add("channel_id", _mUserId);
jsonPayload.Add("user_id", _mUserId);
jsonPayload.Add("role", "external");
JObject pubsubPerms = new JObject();
JArray send = new JArray();
send.Add("*");
pubsubPerms.Add("send", send);
jsonPayload.Add("pubsub_perms", pubsubPerms);
string payload = jsonPayload.ToString();
JwtPayload jwtPayload = JwtPayload.Deserialize(payload);
var secToken = new JwtSecurityToken(jwtHeader, jwtPayload);
var tokenHandler = new JwtSecurityTokenHandler();
var signedAndEncodedToken = tokenHandler.WriteToken(secToken);
return signedAndEncodedToken;
}