oauth-2.0 购物者RefreshAccessToken始终返回错误参数

9rnv2umw  于 2022-10-31  发布在  其他
关注(0)|答案(5)|浏览(142)

我试过用php curl运行shopee open api RefreshAccessToken来得到一个新的令牌,但是结果总是给我错误的参数代码。
从本文档中:https://open.shopee.com/documents/v2/OpenAPI%202.0%20Overview?module=87&type=2

<?php
      $partner_id   = ...partnerid;
      $partner_key  = ...partnerkey;
      $timest       = time();
      $shop_id      =  ...shopid;
      $refresh_token = ...refresh_token;
      $host         = 'https://partner.shopeemobile.com';
      $path         = '/api/v2/auth/access_token/get';
      $base_string  = sprintf("%s%s%s",$partner_id,$path,$timest);
      $sign         = hash_hmac('sha256', $base_string, $key, false);

      $url  = $host.$path.sprintf("?timestamp=%s&partner_id=%s&sign=%s",$timest,$partner_id,$sign);   

        $data = array( 
        'shop_id' => $shop_id,
        'refresh_token:' => $refresh_token,
        'partner_id:' => $partner_id,
        );

        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => $url,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_SSL_VERIFYPEER => false,
          CURLOPT_SSL_VERIFYHOST => false,
          CURLOPT_POSTFIELDS => $data,    
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json'
          ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        // echo $response;

?>

结果是

{"error":"error_param","message":"error params","request_id":"b61938142425d13e72292xf3b645d62b"}

我的代码哪里出错了,请帮助。

vsmadaxz

vsmadaxz1#

你设法解决它吗?我得到相同的结果,即使在获得访问令牌页。

mf98qq94

mf98qq942#

我不使用php,但在python中,我解决了shop_id和partner_id中将类型str转换为int的问题

body = {
    'code': code,
    'shop_id': int(shop_id),
    'partner_id': int(partner_id),
}

遵循我的完整代码

def get_token_shop_level(code, partner_id, partner_key, shop_id):
    timest = int(time.time())

    body = {
        'code': code,
        'shop_id': int(shop_id),
        'partner_id': int(partner_id),
    }

    host = 'https://partner.test-stable.shopeemobile.com'
    path = '/api/v2/auth/token/get'
    base_string = f'{partner_id}{path}{timest}'

    partner_key = partner_key.encode()
    base_string = base_string.encode()

    sign = hmac.new(partner_key, base_string, hashlib.sha256).hexdigest()
    url = host + path + f'?partner_id={partner_id}&timestamp={timest}&sign={sign}'

    headers = {
        'Content-Type': 'application/json'
    }

    resp = requests.post(url, json=body, headers=headers)
    ret = json.loads(resp.content)
    access_token = ret.get('access_token')
    refresh_token = ret.get('refresh_token')

    return access_token, refresh_token
0lvr5msh

0lvr5msh3#

$partner_id   = ...partnerid;
  $partner_key  = ...partnerkey;
  $timest       = time();
  $shop_id      =  ...shopid;
  $refresh_token = ...refresh_token;
  $host         = 'https://partner.shopeemobile.com';
  $path         = '/api/v2/auth/access_token/get';
  $base_string  = sprintf("%s%s%s",$partner_id,$path,$timest);
  $sign         = hash_hmac('sha256', $base_string, $key, false);

您$符号错误..应该是

$sign         = hash_hmac('sha256', $base_string,  $partner_key, false);
nwlls2ji

nwlls2ji4#

答案在@SungALy和@ DavinKho中,请再次检查您的代码,因为错误提示您的POST数据参数有错误,

$data = array( 
        'shop_id' => $shop_id,
        'refresh_token' => $refresh_token,
        'partner_id' => $partner_id,
        );
$data = json_encode($data);

在传递@SungALy所指向的内容之前,删除数组名称上的冒号并转换为JSON
和$键入

$sign         = hash_hmac('sha256', $base_string, $key, false);

必须是$partner_key

$sign         = hash_hmac('sha256', $base_string, $partner_key, false);

由Davin Kho指出。

yc0p9oo0

yc0p9oo05#

这是为me.in URL工作的,我只是添加**&is_developer=1**和基本字符串,我使用utf8_encode。
$时间戳=时间();
访问令牌的访问权限。
$base = $合作伙伴ID.$路径.$时间戳;
如果您有任何问题,请联系我们。
(“?时间戳=%s&合作伙伴标识=% s &符号= % s&是开发者= 1 '”,$时间戳,$合作伙伴标识,$符号);
并在更改此数据时,使用函数curLOPT_POSTFIELDS =〉json_encode($data),

相关问题