-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsign_api.go
48 lines (45 loc) · 1.08 KB
/
sign_api.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
package getuipush
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"github.com/zituocn/getui-push/models"
"time"
)
// getToken 获取个推token
// 返回token和可能的错误
func getToken(appId, appKey, masterSecret string) (token string, err error) {
sign, timestamp := signature(appKey, masterSecret)
param := &models.TokenParam{
Sign: sign,
Timestamp: fmt.Sprintf("%d", timestamp),
AppKey: appKey,
}
bodyByte, err := makeReqBody(param)
if err != nil {
return
}
b, err := HttpRequest("POST", appId+"/auth", "", bodyByte)
if err != nil {
return
}
resp := new(models.TokenResp)
err = json.Unmarshal(b, &resp)
if resp.Data.Token == "" {
err = errors.New("返回的token为空")
return
}
token = resp.Data.Token
return
}
// signature 生成签名方法
func signature(appKey, masterSecret string) (sign string, timestamp int) {
timestamp = int(time.Now().Unix() * 1000)
original := fmt.Sprintf("%s%d%s", appKey, timestamp, masterSecret)
hash := sha256.New()
hash.Write([]byte(original))
sum := hash.Sum(nil)
sign = fmt.Sprintf("%x", sum)
return
}