前后端分离下 后端请求授权 回调地址填写前端路由 监听到url里面存在code参数。则请求授权回调接口。
授权接口
func (*WeiBo)WeiBoCallBack (c *gin.Context) {
code := c.Query("code")
if len(code) ==0 {
c.JSON(http.StatusForbidden,map[string]interface{}{"msg": "参数不正确~","code":code,})}
access_token := oauth.GetAccessToken(&code)
UserInfo := oauth.GetUserInfo(&access_token)
fmt.Println(UserInfo)
}
微博授权服务类
package oauth
import ("fmt""github.com/tidwall/gjson""go_im/pkg/config""go_im/pkg/helpler""io/ioutil""net/http""net/url""strings"
)
var client_id = config.GetString("oauth.client_id")
var client_secret = config.GetString("oauth.client_secret")
var redirect_uri = config.GetString("oauth.redirect_uri")
var access_token_url = "https://api.weibo.com/oauth2/access_token"
var user_info_url = "https://api.weibo.com/2/users/show.json"
var get_token_info="https://api.weibo.com/oauth2/get_token_info"
type UserInfo struct {
Name string
Email string
Avatar string
OauthId string
BoundOauth int
}
func GetAccessToken(code *string) string {
queryData :=url.Values{"client_id":{client_id},"code":{*code},"client_secret":{client_secret},"redirect_uri":{redirect_uri},"grant_type":{"authorization_code"}}
urls :=access_token_url+"?"+helpler.HttpBuildQuery(queryData)
data := url.Values{"app_id":{"xxx"}}
body := strings.NewReader(data.Encode())
resp,err := http.Post(urls,"application/x-www-form-urlencoded",body)if err!=nil{
fmt.Println(err)}defer resp.Body.Close()
bodyC, _ := ioutil.ReadAll(resp.Body)
access_token := gjson.Get(string(bodyC),"access_token")
return access_token.Str
}
func GetUserInfo(access_token *string) string {
uid :=getUid(&*access_token)
fmt.Println("uid",uid)
urls := user_info_url+"?uid="+ uid+"&access_token="+*access_token
fmt.Println("urls",urls)
resp,err := http.Get(urls)if err!=nil{
fmt.Println(err)}defer resp.Body.Close()
bodyC, _ := ioutil.ReadAll(resp.Body)
return string(bodyC)
}
func getUid(access_token *string) string {
urls := get_token_info+"?access_token="+*access_token
data := url.Values{"app_id":{"xxx"}}
body := strings.NewReader(data.Encode())
resp,err := http.Post(urls,"application/x-www-form-urlencoded",body)
if err!=nil{
fmt.Println(err)}
defer resp.Body.Close()
bodyC, _ := ioutil.ReadAll(resp.Body)
fmt.Println("bodyC",string(bodyC))
uid := gjson.Get(string(bodyC),"uid")
return uid.Raw
}
打印获取到的数据