Shopee RefreshAccessToken 总是返回错误参数

问题描述 投票:0回答:5

我尝试通过运行shopee open api RefreshAccessToken和php curl来获取新令牌, 但结果总是给我错误参数代码。

来自此文档: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"}

我的代码哪里出了问题,请帮忙。

php oauth-2.0 access-token shopee
5个回答
2
投票

我不使用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

0
投票
  $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);

0
投票

答案在@SungALy 和@Davin Kho 中,再次检查你的代码, 正如错误所示,您的 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 指出。


0
投票

这对我有用。在网址中我只需添加 &is_developer=1 和我使用 utf8_encode 的基本字符串。

$时间戳=时间();

$path = '/api/v2/auth/access_token/get';
$base = $partnerId.$path.$timestamp;
$sign = hash_hmac('sha256',utf8_encode($base), $partnerKey);

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

并更改此 CURLOPT_POSTFIELDS => $data,其中 CURLOPT_POSTFIELDS => json_encode($data),


0
投票

您必须检查 json 编码是否未使用此参数(PRESERVE ZERO FRANCTION)或其他可以将令牌从字符串更改为 int/float 或在数字末尾添加“.00”的参数。

json_encode($POST_DATA, JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION);

对我来说,当我删除上面的参数时,它就起作用了

检查最终数据是否确实如此!

注意。

© www.soinside.com 2019 - 2024. All rights reserved.