Import the JWT package
import "github.com/dgrijalva/jwt-go"
Then generate with NewWithClaims
.
Finally, get the signed JWT token string by calling SignedString()
.
package main | |
import ( | |
"fmt" | |
jwt "github.com/dgrijalva/jwt-go" | |
"log" | |
"time" | |
) | |
const ( | |
ApiKey = "123123123" | |
ApiSecret = "secret" | |
) | |
func main() { | |
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | |
"iat": time.Now().Unix(), | |
"sub": ApiKey, | |
}) | |
if token == nil { | |
log.Fatal("error generating token") | |
return | |
} | |
tokenString, err := token.SignedString([]byte(ApiSecret)) | |
if err != nil { | |
log.Fatal("error signing token") | |
return | |
} | |
fmt.Println("Generated JWT token string: " + tokenString) | |
} |