前言
目标:串接虾皮订单、标籤资讯,目前串接虾皮 OpenAPI 2.0 版本,串接手册
串接步骤:
前一篇已经将商店授权,接下来这个步骤取得 Access token(之后其他 API 都会需要)
要取得 access token,你会需要上一个步骤商店授权后获得的 code、shop_id
取得 Access Token
参数说明:虾皮的 API 有两种参数:公共参数、业务参数
公共参数:要带到 url 的 query 上业务参数:依请求方式 POST、GET 带到 BODY 或 Query公共参数:
业务参数:(因为是 POST,所以要带到 Body)
code 时效只有10 分钟,如果过期就必须重新将商店授权
shop_id、main_account_id 二选一,授权商店只收到 shop_id 的话,就只要带 shop_id 就好
以 PHP 为例:
// 正式区$host='https://partner.shopeemobile.com'; $partnerId=xxxxxx;$partnerKey='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // call api 时作为加密key$timestamp=xxxxxxxxxxx;$shop_id='xxxxxxxxxxxxx';$$code='xxxxxxxxxxxxx';// 取得 access tokenfunction getAccessToken( $host, $partnerId, $partnerKey, $timestamp, $shop_id, $code ) {$path='/api/v2/auth/token/get'; $base_string=strval($partnerId.$path.$timestamp);$sign=hash_hmac('sha256',$base_string,$partnerKey,false);$url=$host.$path.' ?partner_id='.$partnerId.'×tamp='.$timestamp.'&sign='.$sign; $Payload='{"code":"'.$code.'","partner_id":'.$partnerId.',"shop_id":'.$shop_id.'}';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_POSTFIELDS, $Payload);curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);$res = curl_exec($ch);curl_close($ch);return $res;}getAccessToken($host,$partnerId,$partnerKey,$timestamp,$shop_id,$code);
Response
有了 access_token 就可以打其他 API 了!
因为 access_token 四小时候就会过期,所以要用 refresh_token 去重取新的 access token
重取 access token (Refresh Token)
重取 access token 跟上一步取得 access token 是不一样的 url,带的参数也略有不同
公共参数:
业务参数:(因为是 POST,所以要带到 Body)
上一步取得access token 是要带 code,重取则是要带 refresh_token,
Response
知道如何重取 token 后也大概熟悉了虾皮 API 的模式,下一篇就可以来取得虾皮的资料啦~
参考资料:
[中文版] OpenAPI 2.0 Overview
(更详细可以参考虾皮的手册)