feat: token store is done but not tested
This commit is contained in:
@@ -1 +1,61 @@
|
||||
package channel
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"message-pusher/common"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type wechatTestAccountResponse struct {
|
||||
ErrorCode int `json:"errcode"`
|
||||
ErrorMessage string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
type WeChatTestAccountTokenStoreItem struct {
|
||||
AppID string
|
||||
AppSecret string
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func (i *WeChatTestAccountTokenStoreItem) Key() string {
|
||||
return i.AppID + i.AppSecret
|
||||
}
|
||||
|
||||
func (i *WeChatTestAccountTokenStoreItem) Token() string {
|
||||
return i.AccessToken
|
||||
}
|
||||
|
||||
func (i *WeChatTestAccountTokenStoreItem) Refresh() {
|
||||
// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
|
||||
client := http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
|
||||
i.AppID, i.AppSecret), nil)
|
||||
if err != nil {
|
||||
common.SysError(err.Error())
|
||||
return
|
||||
}
|
||||
responseData, err := client.Do(req)
|
||||
if err != nil {
|
||||
common.SysError("failed to refresh access token: " + err.Error())
|
||||
return
|
||||
}
|
||||
defer responseData.Body.Close()
|
||||
var res wechatTestAccountResponse
|
||||
err = json.NewDecoder(responseData.Body).Decode(&res)
|
||||
if err != nil {
|
||||
common.SysError("failed to decode wechatTestAccountResponse: " + err.Error())
|
||||
return
|
||||
}
|
||||
if res.ErrorCode != 0 {
|
||||
common.SysError(res.ErrorMessage)
|
||||
return
|
||||
}
|
||||
i.AccessToken = res.AccessToken
|
||||
common.SysLog("access token refreshed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user