- GetUserInfoByCode

This commit is contained in:
gazebo
2019-03-08 10:50:53 +08:00
parent a107ca67d3
commit 0bd52f54a9
3 changed files with 44 additions and 6 deletions

View File

@@ -1,9 +1,13 @@
package dingdingapi
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"net/http"
"strings"
"sync"
"time"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
@@ -74,7 +78,7 @@ func (a *API) GetToken() string {
}
func (a *API) RetrieveToken() (token string, err error) {
result, err := a.AccessAPI(getTokenAction, nil, "")
result, err := a.AccessAPI(getTokenAction, nil, nil)
if err != nil {
return "", err
}
@@ -83,7 +87,13 @@ func (a *API) RetrieveToken() (token string, err error) {
return token, nil
}
func (a *API) AccessAPI(action string, params map[string]interface{}, body string) (retVal map[string]interface{}, err error) {
func (a *API) signParams(timestamp int64) string {
mac := hmac.New(sha256.New, []byte(a.GetSecret()))
mac.Write([]byte(utils.Int64ToStr(timestamp)))
return string(mac.Sum(nil))
}
func (a *API) AccessAPI(action string, params map[string]interface{}, bodyMap map[string]interface{}) (retVal map[string]interface{}, err error) {
params2 := make(map[string]interface{})
for k, v := range params {
params2[k] = v
@@ -100,6 +110,11 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
panic("token is empty!")
}
params2["access_token"] = accessToken
} else {
params2["appkey"] = a.GetAppID()
timestamp := time.Now().Unix()
params2["timestamp"] = timestamp
params2["signature"] = a.signParams(timestamp)
}
fullURL := utils.GenerateGetURL(prodURL, action, params2)
@@ -108,10 +123,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
var request *http.Request
if body == "" {
if bodyMap == nil {
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
} else {
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body))
request, _ = http.NewRequest(http.MethodPost, fullURL, bytes.NewReader(utils.MustMarshal(bodyMap)))
}
return request
},