diff --git a/platformapi/fnpsapi/fnpsapi.go b/platformapi/fnpsapi/fnpsapi.go index 19ed7591..6560f00c 100644 --- a/platformapi/fnpsapi/fnpsapi.go +++ b/platformapi/fnpsapi/fnpsapi.go @@ -133,21 +133,21 @@ func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string] // 获取access_token func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) { parameter := make(map[string]interface{}, 6) - parameter["grant_type"] = a.grantType + parameter["grant_type"] = "refresh_token" parameter["app_id"] = a.appID parameter["merchant_id"] = a.merchantId // 先去刷新token,没有的话再去获取token(code只能使用一次,生成的token管一年) var result map[string]interface{} - parameter["refresh_token"] = a.refreshToken - if a.refreshToken == "" { + if a.accessToken != "" && a.refreshToken != "" { + result, err = a.AccessAPI(RefreshTokenUrl, "", RequestPost, parameter) + } else { + parameter["grant_type"] = "authorization_code" parameter["code"] = a.code result, err = a.AccessAPI(TokenURL, "", RequestPost, parameter) - if err != nil { - return nil, err - } - } else { - result, err = a.AccessAPI(RefreshTokenUrl, "", RequestPost, parameter) + } + if err != nil { + return nil, err } if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil { diff --git a/platformapi/fnpsapi_v3/fnps_v3.go b/platformapi/fnpsapi_v3/fnps_v3.go index 7d8f0352..be12d4e0 100644 --- a/platformapi/fnpsapi_v3/fnps_v3.go +++ b/platformapi/fnpsapi_v3/fnps_v3.go @@ -18,18 +18,19 @@ const ( // 注册请求api type API struct { - grantType string `json:"grant_type"` - code string `json:"code"` - appID string `json:"app_id"` - merchantId string `json:"merchant_id"` - signature string `json:"signature"` - timestamp int64 `json:"timestamp"` - accessToken string `json:"access_token"` - appSecret string `json:"app_secret"` - version string `json:"version"` - locker sync.RWMutex - client *http.Client - config *platformapi.APIConfig + grantType string `json:"grant_type"` + code string `json:"code"` + appID string `json:"app_id"` + merchantId string `json:"merchant_id"` + signature string `json:"signature"` + timestamp int64 `json:"timestamp"` + accessToken string `json:"access_token"` + refreshToken string `json:"refresh_token"` + appSecret string `json:"app_secret"` + version string `json:"version"` + locker sync.RWMutex + client *http.Client + config *platformapi.APIConfig } // 请求基础结构体 diff --git a/platformapi/fnpsapi_v3/fnpsapi.go b/platformapi/fnpsapi_v3/fnpsapi.go index ac4568c9..bf845aec 100644 --- a/platformapi/fnpsapi_v3/fnpsapi.go +++ b/platformapi/fnpsapi_v3/fnpsapi.go @@ -74,18 +74,18 @@ func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) { parameter["grant_type"] = "refresh_token" parameter["app_id"] = a.appID parameter["merchant_id"] = a.merchantId - parameter["refresh_token"] = "1" // 先去刷新token,没有的话再去获取token(code只能使用一次,生成的token管一年) var result map[string]interface{} - result, err = a.AccessAPI(RefreshTokenUrl, "", RequestPost, parameter) - if err != nil { + if a.accessToken != "" && a.refreshToken != "" { + result, err = a.AccessAPI(RefreshTokenUrl, "", RequestPost, parameter) + } else { parameter["grant_type"] = "authorization_code" parameter["code"] = a.code result, err = a.AccessAPI(TokenURL, "", RequestPost, parameter) - if err != nil { - return nil, err - } + } + if err != nil { + return nil, err } if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil {