「新手上路」Go 微博授权登录

前后端分离下 后端请求授权 回调地址填写前端路由 监听到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 协议》,转载必须注明作者和本文链接
不成大牛,不改個簽
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!