forked from hugozhu/godingtalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_sns.go
109 lines (88 loc) · 2.72 KB
/
api_sns.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
//普通钉钉用户账号开放相关接口
package godingtalk
import(
"net/url"
)
//获取钉钉开放应用ACCESS_TOKEN
//TODO:
// 根据和赤司(钉钉开发者)的沟通,ACCESS_TOKEN只有两个小时的有效期
// 但是目前接口貌似没有返回过期时间相关的信息,因此所有相关的调用都需要强制刷新
func (c *DingTalkClient) RefreshSnsAccessToken() error {
var data AccessTokenResponse
params := url.Values{}
params.Add("appid", c.SnsAppID)
params.Add("appsecret", c.SnsAppSecret)
err := c.httpRPC("sns/gettoken", params, nil, &data)
if err==nil {
c.SnsAccessToken = data.AccessToken
}
return err
}
//获取用户授权的持久授权码返回信息
type SnsPersistentCodeResponse struct {
OAPIResponse
UnionID string `json:"unionid"`
OpenID string `json:"openid"`
PersistentCode string `json:"persistent_code"`
}
//获取用户授权的持久授权码
func (c *DingTalkClient) GetSnsPersistentCode(tmpAuthCode string) (string, string, string, error) {
c.RefreshSnsAccessToken()
params := url.Values{}
params.Add("access_token", c.SnsAccessToken)
request := map[string]interface{}{
"tmp_auth_code": tmpAuthCode,
}
var data SnsPersistentCodeResponse
err := c.httpRequest("sns/get_persistent_code", params, request, &data)
if err!=nil {
return "","","",err
}
return data.UnionID, data.OpenID, data.PersistentCode, nil
}
type SnsTokenResponse struct {
OAPIResponse
Expires int `json:"expires_in"`
SnsToken string `json:"sns_token"`
}
//获取用户授权的SNS_TOKEN
func (c *DingTalkClient) GetSnsToken(openid, persistentCode string) (string, error) {
c.RefreshSnsAccessToken()
params := url.Values{}
params.Add("access_token", c.SnsAccessToken)
request := map[string]interface{}{
"openid": openid,
"persistent_code": persistentCode,
}
var data SnsTokenResponse
err := c.httpRequest("sns/get_sns_token", params, request, &data)
if err!=nil {
return "", err
}
return data.SnsToken, err
}
type SnsUserInfoResponse struct {
OAPIResponse
CorpInfo []struct{
CorpName string `json:"corp_name"`
IsAuth bool `json:"is_auth"`
IsManager bool `json:"is_manager"`
RightsLevel int `json:"rights_level"`
} `json:"corp_info"`
UserInfo struct {
MaskedMobile string `json:"marskedMobile"`
Nick string `json:"nick"`
OpenID string `json:"openid"`
UnionID string `json:"unionid"`
DingID string `json:"dingId"`
} `json:"user_info"`
}
//获取用户授权的个人信息
func (c *DingTalkClient) GetSnsUserInfo(snsToken string) (SnsUserInfoResponse, error) {
c.RefreshSnsAccessToken()
params := url.Values{}
params.Add("sns_token", snsToken)
var data SnsUserInfoResponse
err := c.httpRequest("sns/getuserinfo", params, nil, &data)
return data, err
}