- elm and weixin token refresh.

This commit is contained in:
gazebo
2018-06-26 15:26:39 +08:00
parent 910c9e02d1
commit ee4e88594d
8 changed files with 241 additions and 30 deletions

View File

@@ -2,6 +2,7 @@ package elmapi
import (
"crypto/md5"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
@@ -15,9 +16,13 @@ import (
)
const (
sandboxURL = "https://open-api-sandbox.shop.ele.me/api/v1/"
prodURL = "https://open-api.shop.ele.me/api/v1/"
signKey = "signature"
sandboxURL = "https://open-api-sandbox.shop.ele.me/api/v1"
snadboxTokenURL = "https://open-api-sandbox.shop.ele.me/token"
prodURL = "https://open-api.shop.ele.me/api/v1"
prodTokenURL = "https://open-api.shop.ele.me/token"
signKey = "signature"
)
type ResponseResult struct {
@@ -26,10 +31,17 @@ type ResponseResult struct {
Error map[string]interface{}
}
type TokenInfo struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
type API struct {
token string
appKey string
secret string
isProd bool
url *url.URL
client *http.Client
config *platformapi.APIConfig
@@ -54,14 +66,15 @@ func New(token, appKey, secret string, isProd bool, config ...*platformapi.APICo
token: token,
appKey: appKey,
secret: secret,
isProd: isProd,
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
}
if isProd {
api.url, _ = url.Parse(prodURL)
api.url, _ = url.Parse(prodURL + "/")
} else {
api.url, _ = url.Parse(sandboxURL)
api.url, _ = url.Parse(sandboxURL + "/")
}
return api
}
@@ -153,3 +166,41 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
return retVal, err
}
func (a *API) getTokenURL() string {
if a.isProd {
return prodTokenURL
}
return snadboxTokenURL
}
func (a *API) RefreshToken() (retVal *TokenInfo, err error) {
params2 := make(url.Values)
params2["grant_type"] = []string{"client_credentials"}
params2["scope"] = []string{"all"}
// baseapi.SugarLogger.Debug(params2.Encode())
request, _ := http.NewRequest("POST", a.getTokenURL(), strings.NewReader(params2.Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(a.appKey+":"+a.secret)))
err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
retVal = &TokenInfo{}
if accessToken, ok := jsonResult1["access_token"]; ok {
retVal.AccessToken = accessToken.(string)
retVal.TokenType = jsonResult1["token_type"].(string)
retVal.ExpiresIn = int(utils.MustInterface2Int64(jsonResult1["expires_in"]))
// update my token too.
a.token = retVal.AccessToken
return platformapi.ErrLevelSuccess, nil
}
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
})
return retVal, err
}

View File

@@ -20,10 +20,10 @@ func init() {
baseapi.Init(sugarLogger)
// sandbox
// elmapi = New("b4f7e424475c3758c111dc60ceec3e2a", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
elmapi = New("f22acad55e8b11ae146ad9295e20cf4e", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
// prod
elmapi = New("bab2a27f99562f394b411dbb9a6214da", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)
// elmapi = New("bab2a27f99562f394b411dbb9a6214da", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)
}
func TestTest(t *testing.T) {
@@ -62,3 +62,11 @@ func TestCallbackSign(t *testing.T) {
t.Fatal(response, msg)
}
}
func TestRefreshToken(t *testing.T) {
result, err := elmapi.RefreshToken()
if err != nil {
t.Fatal(err.Error())
}
sugarLogger.Debug(result)
}