我正在尝试通过 https://partner.uat.shopeemobile.com/api/v2/shop/get
获取商店详细信息我在 php 中尝试过这个
$timestamp = time();
$partner_key = 123456;
$path = '/api/v2/shop/get';
$access_token = 'token...';
$shop_id = 22222;
$base_string = $partner_key . $path . $timestamp . $access_token . $shop_id;
$secret_key = 'secret...';
$sign = hash_hmac('sha256', $base_string, $secret_key);
但返回的响应是错误的_sign。
我不确定路径是否正确。但这就是我在他们的文档中所理解的。 https://open.shopee.com/documents?module=63&type=2&id=56
$date = new DateTime();
$timestamp = $date->getTimestamp();
$partner_id = "xxxxxx";
$shop_id = "xxxxxx";
$secret_key = "yyyyyyyy";
$path = "/api/v2/auth/token/get"; //without the host
$base_str = $partner_id . $path . $timestamp . $shop_id;
$sign = hash_hmac('sha256', $base_str, $secret_key, false);
Shopee签名取决于您调用的API
对于商店:由partner_id、api路径、时间戳、access_token、shop_id和partner_key通过HMAC-SHA256哈希算法生成的签名。
通过连接partner_id、api_path、timestamp(UNIX格式)、access_token和shop_id来设置基本字符串,然后通过HMAC-SHA256与partner_key对基本字符串进行哈希处理。
设置用于签名创建的变量:
$partner_id = 123456; //app_id of Shopee App
$api_path = '/api/v2/shop/get_shop_info'; //shopee API path
$timestamp = time(); //UNIX format of time, 5mins expiration time
$access_token = '...token'; //access_token from /api/v2/shop/auth_partner
shop_id = '654321' //unique shopee shop id
创建签名
$tokenBaseString = $partner_id.$api_path.$timestamp.$access_token.$shop_id;
$sign = hash_hmac('sha256', $tokenBaseString, $partner_key, false);
您现在可以使用此签名从 Shopee 开放平台创建 GET 请求
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://partner.shopeemobile.com/api/v2/shop/get_shop_info?access_token=$access_token&partner_id=$partner_id&shop_id=$shop_id&sign=$sign×tamp=$timestamp',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
请注意,除了 $api_path 变量中的签名之外,您不能将签名重复用于其他 API 调用,在设置的 $timestamp 的 5 分钟内,您需要在时间跨度到期后创建另一个签名。
注意2* 请务必查看API文档来生成签名,有不同类型的参数用于生成签名。
注 3* Open API v1.0 将于 2022 年底弃用。
这是我用来生成签名的助手。如果您检查 Shopee OP 文档,您会注意到有些标志需要更多参数(私有 API),而有些则需要较少参数(公共 API)。这个 fn 可以帮助促进两者。
public static function buildSignature(string $path, bool $public = false, ?int $timestamp = null): string
{
$timestamp = $timestamp ?? time();
return hash_hmac(
'sha256',
env('SHOPEE_PARTNER_ID') .
$path .
$timestamp ?? time() .
($public ? "" : Cache::get('shopee.auth.access_token', null)) .
($public ? "" : Cache::get('shopee.auth.shop_id', null)),
env('SHOPEE_PARTNER_KEY')
);
}
示例:
要在公共端点使用,您可以像这样使用它:
$sign = buildSignature('/api/v2/auth/token/get', true)
// pass timestamp if you need to sync it with other parts of your request.
$sign = buildSignature('/api/v2/auth/token/get', true, $timestamp)