文档中心 > Taobao Global Open Platform

卖家授权介绍

更新时间:2021/03/24 访问次数:2230

如果您的应用程式需要透过淘宝全球开放平台访问卖家的业务数据(如产品和订单信息),您需要获得卖家的授权,即,访问卖家信息所需要的「Access Token」。您需要指导卖家完成「使用淘宝全球卖家帐号登录授权申请」流程。此过程使用国际 OAuth 2.0 标准协议进行使用者身份验证和授权。

淘宝全球开放平台采用「Code for token」模式,具体描述如下:

服务器地址:https://api.taobao.global/oauth/authorize

授权步骤(如图所示):

image

连接授权 URL

按照以下规则拼装授权URL,并引导客户访问:
https://api.taobao.global/oauth/authorize?response_type=code&redirect_uri=${redirect_uri}&force_auth=true&client_id=${appkey}

请将「appkey」及「redirect_url」替换为您的应用程式设定。

授权 URL 拼接规则:

参数 是否必须? 描述
client_id 由开放平台分配的您的应用程序 App Key。
redirect_url 创建应用程式时提供的回档URL。 redirect_url 是卖方完成授权时回传给您的接收代码,它必须与在淘宝全球开放平台上创建应用程式时提供的回档 URL 相同。
response_type code 值为「code」的授权类型。
force_auth true 刷新 Web 流览器 Cookie 以获得新的授权流程。
state 可自定义,如1212. 应用程式的状态,与输入和回应的相同。
uuid uuid283118319 分配给卖方的身份,可以保护返回的授权码。

引导卖家授权

引导卖家通过 Web 流览器打开上述授权 URL,会出现如下图所示的授权登录页面。左侧显示了授权之后卖家授予应用程式的权限,卖家需输入卖家的帐号和密码,点击「登录并授权」后,完成对应用程式的授权。

image

取得授权码

卖家完成授权后,淘宝全球开放平台会返回授权码到回传 URL 地址。您的应用程式会通过授权码来获取 Access Token。网址将为您所设定的 APP 回档位址 + 授权码(code)

image

注意:
- 此授权码将在 30 分钟内过期。您需要在过期前使用此代码获取 Access token。

取得 Access Token

使用「/auth/token/create API」来获取 Access Token。

代码示例:

IopClient client = new IopClientImpl("https://api.taobao.global/rest", appkey, appSecret); 
IopRequest request = new IopRequest("/auth/token/create"); 
request.addApiParameter("code", "0_TryzT8Vd9T1pwS7VWZ2qlMOS5");
request.addApiParameter("grantType", "authorization_code");
IopResponse response = client.execute(request); 
System.out.println(response.getBody());

保存 Access Token

Access Token 将在一定时间范围内有效。在过期之前,卖家不需要再次授权应用程式。您需要妥善保管最新的 Token。

参看 Token 样例:

{
   "code": "0",
   "access_token": "50000601c30atpedfgu3LVvik87Ixlsvle3mSoB7701ceb156fPunYZ43GBg",
   "refresh_token": "500016000300bwa2WteaQyfwBMnPxurcA0mXGhQdTt18356663CfcDTYpWoi",
   "account_id": "7063844",
   "user_Id": "1001",
   "account_platform": "seller_center",
   "refresh_expires_in": "60",
   "error_code": "error_code",
   "expires_in": "10",
   "request_id": "0ba2887315178178017221014",
   "seller_Id": "10011",
   "account": "xxx@126.com",
   "short_code": "HK001"
}

注:
- 此示例中的「access_token」和「refresh_token」仅供参考。

下表列出了 Token 的参数和描述:

Key Type Sample 描述
access_token string 50000601c30atpedfgu3LVvik87Ixlsvle3mSoB7701ceb156fPunYZ43GBg Access token.
refresh_token string 500016000300bwa2WteaQyfwBMnPxurcA0mXGhQdTt18356663CfcDTYpWoi Refresh token, 用于在「refresh_expires_in」>0 时刷新。
expires_in number 25920 (expires in 25920 seconds) Access token 的过期时间以秒为单位. 「测试」状态的应用程式, 该值是7天. 「在线」状态的应用程式,该值是 30天.
refresh_expires_in number 25920 (expires in 25920 seconds) refresh token的过期时间. 「测试」状态的应用程式,该值是 30天.「在线」状态的应用程式,该值是180天.
account_id string 706388888 User ID, 当 “account_platform” = “seller_center” 时可以忽略。
account string xxx@126.com User account.
account_platform string seller_center 用户平台,支持多个平台。

重新取得授权

使用「/auth/token/refresh」来更新 Access Token。

参看如下样例:

IopClient client = new IopClientImpl("https://api.taobao.global/rest", appkey, appSecret); 
IopRequest request = new IopRequest("/auth/token/refresh"); 
request.addApiParameter("refresh_token", "50000601c30atpedfgu3LVvik87Ixlsvle3mSoB7701ceb156fPunYZ43GBg"); 
IopResponse response = client.execute(request); 
System.out.println(response.getBody());

通过 「/auth/token/refresh」 返回的数据结构与通过授权代码获取访问权限返回的数据结构相同。您将获得新的「access_token」和「refresh_token」。您必须保存最新的「refresh_token」以获取新的「access_token」。请注意,access_token 的时长会被重置,但 refresh_token 的时长不会被重置。refresh_token 过期后,卖家需要重新授权您的应用程式来生成新的 access_token 和 refresh_token。

注意事项:
- 在 Refresh Token 过期前,卖家不需要再次授权。
- 如果 「refresh_expires_in」= 0,则无法刷新 Access Token。只有当 「refresh_expires_in」> 0时,才可以呼叫/auth/token/refresh API 来刷新 access token。
- 如果需要更新 Access Token,建议在 Access Token 过期前 30 分钟进行刷新。

FAQ

关于此文档暂时还没有FAQ
返回
顶部