虾皮串接实作笔记-Access Token

前言

目标:串接虾皮订单、标籤资讯,目前串接虾皮 OpenAPI 2.0 版本,串接手册
串接步骤:

Create App:建立串接的帐号Authorize Shop:商店授权Access Token:取得串接用需要的 access token串接 API

前一篇已经将商店授权,接下来这个步骤取得 Access token(之后其他 API 都会需要)

要取得 access token,你会需要上一个步骤商店授权后获得的 code、shop_id


取得 Access Token

格式HTTP/JSONURL正式区:https://partner.shopeemobile.com/api/v2/auth/token/get测试区:https://partner.test-stable.shopeemobile.com/api/v2/auth/token/get请求方式POST

参数说明:虾皮的 API 有两种参数:公共参数、业务参数

公共参数:要带到 url 的 query 上业务参数:依请求方式 POST、GET 带到 BODY 或 Query

公共参数:

参数类型说明signstringpartner_id、api path、timestamp HMAC-SHA256 编码,并用 partner key 当作加密 Key (可参授权商店那一篇)partner_idintCreate App 产生的 partner_id (可参Create App 那一篇)timestampint时间戳,期限 5 min

业务参数:(因为是 POST,所以要带到 Body)

参数类型说明codestring授权商店后跳转 url 中的 code,一次性,10 min 有效partner_idintCreate App 产生的 partner_id (可参Create App 那一篇)shop_idint授权商店后跳转 url 中的 shop_idmain_account_idint授权商店后跳转 url 中的 main_account_id

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.'&timestamp='.$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

参数类型说明request_idstring每个 request 的 iderrorstring错误码,如果成功会是空的refresh_tokenstringrefresh_token 之后重取 token 会需要,期限 30 天access_tokenstring授权码,可多次使用,4小时失效expire_inintaccess token 的有效时间(秒)messagestring错误讯息merchant_id_listint[]request 有带 main_account_id 才有,main account 下的所有 merchant_idshop_id_listint[]request 有带 main_account_id 才有,main account 下的所有 shop_id

有了 access_token 就可以打其他 API 了!
因为 access_token 四小时候就会过期,所以要用 refresh_token 去重取新的 access token


重取 access token (Refresh Token)

重取 access token 跟上一步取得 access token 是不一样的 url,带的参数也略有不同
公共参数:

格式HTTP/JSONURL正式区:https://partner.shopeemobile.com/api/v2/auth/access_token/get测试区:https://partner.test-stable.shopeemobile.com/api/v2/auth/access_token/get请求方式POST

业务参数:(因为是 POST,所以要带到 Body)

参数类型说明signstringpartner_id、api path、timestamp HMAC-SHA256 编码,并用 partner key 当作加密 Key (可参授权商店那一篇)partner_idintCreate App 产生的 partner_id (可参Create App 那一篇)timestampint时间戳,期限 5 min

上一步取得access token 是要带 code,重取则是要带 refresh_token,

参数类型说明refresh_tokenstring在取得 access token 时产生的 refresh_tokenpartner_idintCreate App 产生的 partner_id (可参Create App 那一篇)shop_idint授权商店后跳转 url 中的 shop_idmerchant_idint授权给开发者的 merchant_idshop_id、merchant_id 二择一填入

Response

参数类型说明request_idstring每个 request 的 iderrorstring错误码,如果成功会是空的refresh_tokenstringrefresh_token 之后重取 token 会需要,期限 30 天access_tokenstring授权码,可多次使用,4小时失效expire_inintaccess token 的有效时间(秒)partner_idintcreate App 取得的 partner_idshop_idint授权 url 取得的 shop_idmerchant_idintrequest 有带 main_account_id 才有,main account 下的所有 merchant_id

知道如何重取 token 后也大概熟悉了虾皮 API 的模式,下一篇就可以来取得虾皮的资料啦~

参考资料:

[中文版] OpenAPI 2.0 Overview
(更详细可以参考虾皮的手册)


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章