package main import ( "net/http" "github.com/gin-gonic/gin" "github.com/go-oauth2/gin-server" "gopkg.in/oauth2.v3/manage" "gopkg.in/oauth2.v3/models" "gopkg.in/oauth2.v3/server" "gopkg.in/oauth2.v3/store" ) func main() { manager := manage.NewDefaultManager() // token store manager.MustTokenStorage(store.NewFileTokenStore("data.db")) // client store clientStore := store.NewClientStore() clientStore.Set("000000", &models.Client{ ID: "000000", Secret: "999999", Domain: "http://localhost", }) manager.MapClientStorage(clientStore) // Initialize the oauth2 service ginserver.InitServer(manager) ginserver.SetAllowGetAccessRequest(true) ginserver.SetClientInfoHandler(server.ClientFormHandler) g := gin.Default() auth := g.Group("/oauth2") { auth.GET("/token", ginserver.HandleTokenRequest) } api := g.Group("/api") { api.Use(ginserver.HandleTokenVerify()) api.GET("/test", func(c *gin.Context) { ti, exists := c.Get(ginserver.DefaultConfig.TokenKey) if exists { c.JSON(http.StatusOK, ti) return } c.String(http.StatusOK, "not found") }) } g.Run(":9096") }