-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccesstoken.go
70 lines (64 loc) · 1.6 KB
/
accesstoken.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
package dingtalk
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func NewDingtalkByJson(CorpId, AppKey, AppSecret string) DingTalk {
data, err := ioutil.ReadFile("token.json")
dt := DingTalk{CorpId: CorpId, AppKey: AppKey, AppSecret: AppSecret}
var tk Token
if err == nil {
json.Unmarshal(data, &tk)
dt.AccessToken = tk.AccessToken
dt.ExpiresIn = tk.ExpiresIn
}
return dt
}
func NewDingtalk(AppKey, AppSecret string) DingTalk {
return DingTalk{AppKey: AppKey, AppSecret: AppSecret}
}
func (this *DingTalk) GetToken() string {
nowTime := time.Now().Unix()
if nowTime > this.ExpiresIn {
err := this.getToken()
if err != nil {
fmt.Print("Token 重新获取失败\n")
return ""
} else {
this.WriteToFile()
return this.AccessToken
}
} else {
return this.AccessToken
}
}
//发送网络请求
func (this *DingTalk) getToken() error {
client := &http.Client{}
var msg AccessTokenMsg
FullUrl := fmt.Sprintf(GetTokenUrl, this.AppKey, this.AppSecret)
request, _ := http.NewRequest("GET", FullUrl, nil)
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
json.Unmarshal(body, &msg)
nowTime := time.Now().Unix()
this.ExpiresIn = msg.ExpiresIn + nowTime
this.AccessToken = msg.AccessToken
return nil
}
//token存入文件缓存
func (this *DingTalk) WriteToFile() {
tk := Token{this.AccessToken, this.ExpiresIn}
data, _ := json.Marshal(tk)
ioutil.WriteFile("token.json", data, 0666)
}