-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoauth_qq.go
107 lines (95 loc) · 2.86 KB
/
oauth_qq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package simpleoauth
import (
"httplib"
"strings"
"encoding/json"
)
const qq_getaccesstoken_url = "https://graph.qq.com/oauth2.0/token"
const qq_getuserinfo_url = "https://graph.qq.com/user/get_user_info"
const qq_openid_url = "https://graph.qq.com/oauth2.0/me"
var qqOAuth = &QQOAuth{}
type QQOAuth struct {
appkey string
appsecret string
redirect_url string
}
func (oauth *QQOAuth) GetAccesstoken(code string) map[string]interface{}{
request:= httplib.Get(qq_getaccesstoken_url)
request.Param("grant_type","authorization_code")
request.Param("client_id",oauth.appkey)
request.Param("client_secret",oauth.appsecret)
request.Param("code",code)
request.Param("redirect_uri", oauth.redirect_url)
response, err := request.String()
if err != nil {
return nil
}
if strings.Contains(response, "callback"){
return nil
}
temp := strings.Split(response, "&")[0]
accesstoken := strings.Split(temp, "=")[1]
return map[string]interface{}{"access_token" : accesstoken}
}
func (oauth *QQOAuth) GetOpenid(accesstoken string) map[string]interface{}{
request:= httplib.Get(qq_openid_url)
request.Param("access_token",accesstoken)
request.Param("unionid","1")
responseStr, _ := request.String()
var response map[string]interface{}
json.Unmarshal([]byte(responseStr[10:len(responseStr)-3]),&response)
return response
}
func (oauth *QQOAuth) GetUserinfo(accesstoken string, openid string) map[string]interface{}{
request:= httplib.Get(qq_getuserinfo_url)
request.Param("access_token",accesstoken)
request.Param("oauth_consumer_key",oauth.appkey)
request.Param("openid",openid)
var response map[string]interface{}
err := request.ToJson(&response)
if err != nil {
return nil
}
return response
}
func (oauth *QQOAuth) Authorize(code string) AuthorizeResult{
accesstokenResponse := oauth.GetAccesstoken(code)
if accesstokenResponse == nil{
return AuthorizeResult{false, nil}
}
accesstoken := accesstokenResponse["access_token"].(string)
openidResponse := oauth.GetOpenid(accesstoken)
if _, ok := openidResponse["error"]; ok { //获取openid接口返回错误
return AuthorizeResult{false, nil}
}
openid := openidResponse["openid"].(string)
unionid := openidResponse["unionid"].(string)
getuserinfoResult := oauth.GetUserinfo(accesstoken, openid)
if getuserinfoResult == nil{
return AuthorizeResult{false, nil}
}
var sex int
gender, ok := getuserinfoResult["gender"]
if !ok {
sex = 1
}
if gender.(string) == "女" {
sex = 2
}else{
sex = 1
}
return AuthorizeResult{true, map[string]interface{}{
"nickname":getuserinfoResult["nickname"].(string),
"openid":openid,
"sex":sex,
"headimgurl":getuserinfoResult["figureurl_qq_1"].(string), // QQ头像 40x40尺寸
"unionid":unionid}}
}
func (oauth *QQOAuth) InitOAuth(){
oauth.appkey = QQappkey
oauth.appsecret = QQappsecret
oauth.redirect_url = QQRedirectUrl
}
func init(){
ReisterPlatform("qq", qqOAuth)
}