package weixinrpc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.tetele.net/tgo/crypter"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
func GetAccessToken(dbname, site_id string, appid, secret string, url ...string) (*AccessTokenRes, error) {
|
|
|
|
conn, err := rpc_server_conn(url...)
|
|
|
|
data := AccessTokenReq{SiteId: site_id, Dbname: dbname, Appid: appid, Secret: secret}
|
|
|
|
data_json, err := json.Marshal(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
encryData := crypter.DesEn(string(data_json), DES_KEY)
|
|
|
|
now_int64 := time.Now().Unix()
|
|
|
|
now := strconv.FormatInt(now_int64, 10)
|
|
|
|
sign := Sign(encryData, now)
|
|
|
|
req := &Request{
|
|
proto.String(encryData),
|
|
proto.String(now),
|
|
proto.String(sign),
|
|
nil}
|
|
|
|
res := &Response{}
|
|
|
|
err = conn.GetAccessToken(req, res)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return HandleAccessToken(res)
|
|
|
|
}
|
|
|
|
/**
|
|
* 处理返回结果
|
|
*/
|
|
func HandleAccessToken(res *Response) (*AccessTokenRes, error) {
|
|
|
|
//解密
|
|
res_data_de, err := GetOrgData(res)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var res_arr AccessTokenRes
|
|
|
|
err = json.Unmarshal([]byte(res_data_de), &res_arr)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &res_arr, nil
|
|
}
|