虾皮串接实作笔记-Access Token

前言

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

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

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

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


取得 Access Token

格式 HTTP/JSON
URL 正式区: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

公共参数:

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

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

参数 类型 说明
code string 授权商店後跳转 url 中的 code,一次性,10 min 有效
partner_id int Create App 产生的 partner_id (可参Create App 那一篇)
shop_id int 授权商店後跳转 url 中的 shop_id
main_account_id int 授权商店後跳转 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 token
function 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_id string 每个 request 的 id
error string 错误码,如果成功会是空的
refresh_token string refresh_token 之後重取 token 会需要,期限 30 天
access_token string 授权码,可多次使用,4小时失效
expire_in int access token 的有效时间(秒)
message string 错误讯息
merchant_id_list int[] request 有带 main_account_id 才有,main account 下的所有 merchant_id
shop_id_list int[] 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/JSON
URL 正式区: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)

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

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

参数 类型 说明
refresh_token string 在取得 access token 时产生的 refresh_token
partner_id int Create App 产生的 partner_id (可参Create App 那一篇)
shop_id int 授权商店後跳转 url 中的 shop_id
merchant_id int 授权给开发者的 merchant_id

shop_id、merchant_id 二择一填入

Response

参数 类型 说明
request_id string 每个 request 的 id
error string 错误码,如果成功会是空的
refresh_token string refresh_token 之後重取 token 会需要,期限 30 天
access_token string 授权码,可多次使用,4小时失效
expire_in int access token 的有效时间(秒)
partner_id int create App 取得的 partner_id
shop_id int 授权 url 取得的 shop_id
merchant_id int request 有带 main_account_id 才有,main account 下的所有 merchant_id

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

参考资料:

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


<<:  ISO 27001 资讯安全管理系统 【解析】(二十三)

>>:  2022重拾程序-练英打

企划实现(27)

使用spinner并侦测 第一步:在values创建你要放在spinner的资料 第二步:在xml的...

Day-17 Excel手把手作图表(二)

今日练习档 ԅ( ¯་། ¯ԅ) 是的,今天即将延续昨天的部分继续设计Excel图表,如果您还没有看...

JavaScript Day 27. AJAX、Request、Response

如何获取 DOM 节点、串接 API,我们已经大概能了解,可是在网路这个大区域里面,那些传过来传过去...

软件保证成熟度模型(SAMM)-安全冠军(Security Champion)

-SAMM 概述(来源:https : //owaspsamm.org) 软件保障成熟度模型(SA...