美团配送cookie设置
This commit is contained in:
@@ -182,6 +182,7 @@ type API struct {
|
||||
|
||||
appKey string
|
||||
secret string
|
||||
token string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
@@ -227,9 +228,9 @@ func (a *API) signParams(params url.Values) string {
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI2(baseURL, action string, params map[string]interface{}) (retVal *ResponseResult, err error) {
|
||||
if params == nil {
|
||||
panic("params is nil!")
|
||||
}
|
||||
// if params == nil {
|
||||
// panic("params is nil!")
|
||||
// }
|
||||
|
||||
params2 := utils.Map2URLValues(params)
|
||||
if baseURL == mtpsAPIURL {
|
||||
@@ -248,6 +249,7 @@ func (a *API) AccessAPI2(baseURL, action string, params map[string]interface{})
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||
request.Header.Set("Referer", "https://page.peisong.meituan.com/open/admin/poilist")
|
||||
if baseURL != mtpsAPIURL {
|
||||
request.Header.Set("csrfToken", a.token)
|
||||
a.FillRequestCookies(request)
|
||||
}
|
||||
return request
|
||||
|
||||
@@ -25,7 +25,7 @@ func init() {
|
||||
// prod
|
||||
// api = New("3c0a05d464c247c19d7ec13accc78605", "b1M}9?:sTbsB[OF2gNORnN(|(iy9rB8(`7]|[wGLnbmt`evfM>E:A90DjHAW:UPE")
|
||||
|
||||
api.SetCookie("token", "GsfR99YCT8leEBnY39YxPeWTJiSmetA3NGl8G8u1Mv29V4KLYIA9rH3fhw-uDL7VwM4jKPfNwH8D_vOPg3cRYg")
|
||||
api.SetCookie("token", "oo3x6pAld4SskzvbEOnSslWUUQwc6w2m7uCfppCEnvRWzHVqctujL0xzLCkro6qj7mowU51zl99ExVUxgJjJ_Q")
|
||||
}
|
||||
|
||||
func handleError(t *testing.T, err error) {
|
||||
|
||||
@@ -2,10 +2,52 @@ package mtpsapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||
if a.GetCookieCount() == 0 {
|
||||
return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
|
||||
}
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
var request *http.Request
|
||||
if isPost {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(bizParams).Encode()))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||
request.Header.Set("Referer", "https://page.peisong.meituan.com/open/admin/poilist")
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", bizParams), nil)
|
||||
}
|
||||
a.FillRequestCookies(request)
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if strings.Contains(bodyStr, "登录") || strings.Contains(bodyStr, "访问的内容") {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("cookie可能过期了!")
|
||||
}
|
||||
if err == nil {
|
||||
if jsonResult1["error_response"] != nil {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["zh_desc"].(string), jsonResult1["error_response"].(map[string]interface{})["code"].(string))
|
||||
baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
return errLevel, err
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func (a *API) PagePoiUpdate(outerPoiID, contactName, contactPhone, contactEmail string) (err error) {
|
||||
if outerPoiID == "" || contactName == "" || contactPhone == "" || contactEmail == "" {
|
||||
return fmt.Errorf("所有参数必须都要有值")
|
||||
@@ -16,10 +58,21 @@ func (a *API) PagePoiUpdate(outerPoiID, contactName, contactPhone, contactEmail
|
||||
"contactPhone": contactPhone,
|
||||
"contactEmail": contactEmail,
|
||||
}
|
||||
if token, err := a.RefreshToken(); err == nil {
|
||||
a.token = token
|
||||
}
|
||||
_, err = a.AccessAPI2("https://page.peisong.meituan.com/api", "haikuiopen/haikui/open/partner/poi/update", params)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *API) RefreshToken() (token string, err error) {
|
||||
result, err := a.AccessStorePage("https://peisong.meituan.com/api/haikuiopen/haikui/open/auth/csrf/token/refresh", nil, true)
|
||||
if err == nil {
|
||||
token = result["tokens"].(map[string]interface{})["csrfToken"].(string)
|
||||
}
|
||||
return token, err
|
||||
}
|
||||
|
||||
func (a *API) GetAccountDetail() (err error) {
|
||||
params := map[string]interface{}{}
|
||||
_, err = a.AccessAPI2("https://peisong.meituan.com/api", "haikuiopen/haikui/open/partner/base/detail", params)
|
||||
|
||||
@@ -27,3 +27,11 @@ func TestGetStoreStatus(t *testing.T) {
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func TestRefreshToken(t *testing.T) {
|
||||
result, err := api.RefreshToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user