MCPcopy Index your code
hub / github.com/TruthHun/BookStack / GetQQOpenId

Function GetQQOpenId

oauth/qq.go:65–105  ·  view source on GitHub ↗

获取OpenId

(token QQAccessToken)

Source from the content-addressed store, hash-verified

63
64//获取OpenId
65func GetQQOpenId(token QQAccessToken) (openid string, err error) {
66 var resp string
67 Api := beego.AppConfig.String("oauth::qqOpenId") + "?access_token=" + token.AccessToken
68 req := httplib.Get(Api)
69 if strings.HasPrefix(Api, "https") {
70 req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
71 }
72 //更奇葩的是这里,我明明是PC的接口,但是返回的确实移动端的数据格式。。
73 if resp, err = req.String(); err == nil {
74 if debug {
75 beego.Debug("获取QQ登录的openid", resp, Api)
76 }
77 if strings.Contains(resp, "callback(") { //callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
78 var data struct {
79 Openid string `json:"openid"`
80 }
81 var js string
82 if slice := strings.Split(strings.Split(resp, "}")[0]+"}", "{"); len(slice) == 2 {
83 js = "{" + slice[1]
84 }
85 if err = json.Unmarshal([]byte(js), &data); err == nil {
86 openid = data.Openid
87 return
88 } else {
89 beego.Error("解析出错", err.Error())
90 }
91 } else {
92 if slice := strings.Split(resp, "&"); len(slice) > 0 { //client_id=100222222&openid=1704************************878C
93 for _, item := range slice {
94 if strings.HasPrefix(item, "openid=") {
95 openid = strings.TrimPrefix(item, "openid=")
96 return
97 }
98 }
99 } else {
100 err = errors.New("获取授权失败,请使用QQ重新获取授权登录")
101 }
102 }
103 }
104 return
105}
106
107//获取用户信息
108func GetQQUserInfo(accessToken, openid string) (info QQUser, err error) {

Callers 1

OauthMethod · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected