代码拉取完成,页面将自动刷新
package handler
import (
"context"
"gitee.com/jingshanccc/course/gateway/middleware"
"gitee.com/jingshanccc/course/public"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
//Authorize: oauth获取授权码
func Authorize(ctx *gin.Context) {
err := middleware.AuthServer.HandleAuthorizeRequest(ctx.Writer, ctx.Request)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
}
//Token: oauth获取令牌
func Token(ctx *gin.Context) {
err := middleware.AuthServer.HandleTokenRequest(ctx.Writer, ctx.Request)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
}
//Redirect: oauth跳转地址 完成将授权码和client信息重定向到获取令牌接口的工作
func Redirect(ctx *gin.Context) {
client, _ := middleware.AuthServer.Manager.GetClient(context.Background(), ctx.Request.FormValue("state"))
postValue := url.Values{
"code": {ctx.Request.FormValue("code")},
"client_id": {client.GetID()},
"client_secret": {client.GetSecret()},
"grant_type": {"authorization_code"},
"redirect_uri": {"http://" + ctx.Request.Host + strings.Split(ctx.Request.RequestURI, "?")[0]},
}
resp, err := http.PostForm("http://"+ctx.Request.Host+"/api/v1/oauth/token", postValue)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
content := string(body)
public.ResponseSuccess(ctx, content)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。