You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

package main
import (
"OAuth2Client/CommonUtil"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
var (
OAuth2Server = "http://10.10.24.100:8000/"
authCodeURI = OAuth2Server + "oauth2/authorize"
authTokenURI = OAuth2Server + "oauth2/access_token"
clientId = "bpneg1i563uig14hrbf0"
clientSecret = "01e3gkmp9d8z08k78h779dj042"
redirectURI = "http://10.10.24.100:8082/code"
responseType = "code"
grantType = "authorization_code"
)
func main() {
// 开发模式
gin.SetMode(gin.DebugMode)
// 开启gin服务器
r := gin.Default()
r.GET("/", func(context *gin.Context) {
authCodeURL := authCodeURI + "?client_id=" + clientId + "&redirect_uri=" + redirectURI + "&device_id=1&response_type=" + responseType
context.Redirect(http.StatusFound, authCodeURL)
})
r.GET("/code", func(context *gin.Context) {
code := context.Query("code")
authTokenParams := "grant_type=" + grantType + "&client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=" + redirectURI + "&code=" + code
res, _ := CommonUtil.HttpPost(authTokenURI, authTokenParams)
myMap := make(map[string]interface{})
json.Unmarshal([]byte(res), &myMap)
context.JSON(http.StatusOK, myMap)
})
//http://10.10.24.100:8082/refresh_token?refresh_token=fnR6UZTqSjyuEUoSiWOnag
r.GET("/refresh_token", func(context *gin.Context) {
var refreshToken = context.Query("refresh_token")
authTokenParams := "grant_type=refresh_token&refresh_token=" + refreshToken + "&client_id=" + clientId + "&client_secret=" + clientSecret
res, err := CommonUtil.HttpPost(authTokenURI, authTokenParams)
if err != nil {
fmt.Println(err)
}
myMap := make(map[string]interface{})
json.Unmarshal([]byte(res), &myMap)
context.JSON(http.StatusOK, myMap)
})
r.Run(":8082")
}