-
Notifications
You must be signed in to change notification settings - Fork 40
/
authenticator.go
132 lines (114 loc) · 3.26 KB
/
authenticator.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gobo
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// Authenticator结构体实现了微博应用授权功能
type Authenticator struct {
redirectUri string
clientId string
clientSecret string
initialized bool
httpClient *http.Client
}
// 初始化结构体
//
// 在调用其它函数之前必须首先初始化。
func (auth *Authenticator) Init(redirectUri string, clientId string, clientSecret string) error {
// 检查结构体是否已经初始化
if auth.initialized {
return &ErrorString{"Authenticator结构体已经初始化"}
}
auth.redirectUri = redirectUri
auth.clientId = clientId
auth.clientSecret = clientSecret
auth.httpClient = new(http.Client)
auth.initialized = true
return nil
}
// 得到授权URI
func (auth *Authenticator) Authorize() (string, error) {
// 检查结构体是否初始化
if !auth.initialized {
return "", &ErrorString{"Authenticator结构体尚未初始化"}
}
return fmt.Sprintf("%s/oauth2/authorize?redirect_uri=%s&response_type=code&client_id=%s", ApiDomain, auth.redirectUri, auth.clientId), nil
}
// 从授权码得到访问令牌
func (auth *Authenticator) AccessToken(code string) (AccessToken, error) {
// 检查结构体是否初始化
token := AccessToken{}
if !auth.initialized {
return token, &ErrorString{"Authenticator结构体尚未初始化"}
}
// 生成请求URI
queries := url.Values{}
queries.Add("client_id", auth.clientId)
queries.Add("client_secret", auth.clientSecret)
queries.Add("redirect_uri", auth.redirectUri)
queries.Add("grant_type", "authorization_code")
queries.Add("code", code)
// 发送请求
err := auth.sendPostHttpRequest("oauth2/access_token", queries, &token)
return token, err
}
// 得到访问令牌对应的信息
func (auth *Authenticator) GetTokenInfo(token string) (AccessTokenInfo, error) {
// 检查结构体是否初始化
info := AccessTokenInfo{}
if !auth.initialized {
return info, &ErrorString{"Authenticator结构体尚未初始化"}
}
// 生成请求URI
queries := url.Values{}
queries.Add("access_token", token)
// 发送请求
err := auth.sendPostHttpRequest("oauth2/get_token_info", queries, &info)
return info, err
}
// 解除访问令牌的授权
func (auth *Authenticator) Revokeoauth2(token string) error {
// 检查结构体是否初始化
if !auth.initialized {
return &ErrorString{"Authenticator结构体尚未初始化"}
}
// 生成请求URI
queries := url.Values{}
queries.Add("access_token", token)
// 发送请求
type Result struct {
Result string
}
var result Result
err := auth.sendPostHttpRequest("oauth2/revokeoauth2", queries, &result)
return err
}
func (auth *Authenticator) sendPostHttpRequest(apiName string, queries url.Values, response interface{}) error {
// 生成请求URI
requestUri := fmt.Sprintf("%s/%s", ApiDomain, apiName)
// 发送POST Form请求
resp, err := auth.httpClient.PostForm(requestUri, queries)
if err != nil {
return err
}
defer resp.Body.Close()
// 解析返回内容
bytes, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode == 200 {
err := json.Unmarshal(bytes, &response)
if err != nil {
return err
}
} else {
var weiboErr WeiboError
err := json.Unmarshal(bytes, &weiboErr)
if err != nil {
return err
}
return weiboErr
}
return nil
}