「新手上路」Go 微博授權登入

Latent發表於2021-06-22

前後端分離下 後端請求授權 回撥地址填寫前端路由 監聽到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)
    //註冊邏輯以及生產token
}

微博授權服務類



/**
  @author:panliang
  @data:2021/6/21
  @note
**/
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"

// Result represents a json value that is returned from GetUserInfo().

type UserInfo struct {
    Name string
    Email string
    Avatar string
    OauthId string
    BoundOauth int
}

// GetAccessToken function string returns an string access_token.str
// 傳遞指標即可
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
}

// GetUserInfo function  returns an UserInfo

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)

}
// get uid
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
}

列印獲取到的資料

Go

本作品採用《CC 協議》,轉載必須註明作者和本文連結
不成大牛,不改個簽

相關文章