-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaccess_token.go
70 lines (64 loc) · 2.82 KB
/
access_token.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
// https://open.weibo.com/wiki/Oauth2/access_token
// 请求参数:
// client_id true string 申请应用时分配的AppKey。
// client_secret true string 申请应用时分配的AppSecret。
// grant_type true string 请求的类型,填写authorization_code
// grant_type为authorization_code时:
// code true string 调用authorize获得的code值。
// redirect_uri true string 回调地址,需需与注册应用里的回调地址一致。
// 返回数据:
// access_token string 用户授权的唯一票据,用于调用微博的开放接口,同时也是第三方应用验证微博用户登录的唯一票据,第三方应用应该用该票据和自己应用内的用户建立唯一影射关系,来识别登录状态,不能使用本返回值里的UID字段来做登录识别。
// expires_in string access_token的生命周期,单位是秒数。
// remind_in string access_token的生命周期(该参数即将废弃,开发者请使用expires_in)。
// uid string 授权用户的UID,本字段只是为了方便开发者,减少一次user/show接口调用而返回的,第三方应用不能用此字段作为用户登录状态的识别,只有access_token才是用户授权的唯一票据。
package weibo
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/pkg/errors"
)
// RespToken 获取 access token 接口的返回结果
type RespToken struct {
RespError
AccessToken string `json:"access_token"` // access token
ExpiresIn int64 `json:"expires_in"` // ExpiresIn 秒之后 token 过期
UID string `json:"uid"`
IsRealName string `json:"isRealName"`
}
// AccessToken 传入授权码请求 access_token 接口,返回 TokenResp 对象
func (w *Weibo) AccessToken(code string) (*RespToken, error) {
tokenURL := "https://api.weibo.com/oauth2/access_token"
data := url.Values{
"client_id": {w.appkey},
"client_secret": {w.appsecret},
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {w.redirecturi},
}
req, err := http.NewRequest(http.MethodPost, tokenURL, strings.NewReader(data.Encode()))
if err != nil {
return nil, errors.Wrap(err, "weibo AccessToken NewRequest error")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := w.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "weibo AccessToken Do error")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "weibo AccessToken ReadAll error")
}
r := &RespToken{}
if err := json.Unmarshal(body, r); err != nil {
return nil, errors.Wrap(err, "weibo AccessToken Unmarshal error:"+string(body))
}
if r.AccessToken == "" {
return nil, errors.New("weibo AccessToken get token failed." + string(body))
}
w.token = r.AccessToken
return r, nil
}