Skip to content

jwt_encode is not working #282

Answered by oxisto
yingshaoxo asked this question in Q&A
Discussion options

You must be logged in to vote

You are using string as the key for an HMAC. The signing method expects a []byte as seen in the README "The HMAC signing method (HS256,HS384,HS512) expect []byte values for signing and validation" or in the example:

jwt/example_test.go

Lines 16 to 29 in 0d2f0d4

func ExampleNewWithClaims_registeredClaims() {
mySigningKey := []byte("AllYourBase")
// Create the Claims
claims := &jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Unix(1516239022, 0)),
Issuer: "test",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString(mySigningKey)
fmt.Printf("%v %v", ss, err)
//Output: eyJhbGc…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by oxisto
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #281 on March 05, 2023 08:18.