alipay.system.oauth.token(換取授權(quán)訪問令牌)

2018-01-06 19:30 更新

[TOC]

支持第三方代理調(diào)用 換取授權(quán)訪問令牌

公共參數(shù)

請求地址

環(huán)境 HTTPS請求地址
正式環(huán)境 https://openapi.alipay.com/gateway.do

公共請求參數(shù)

參數(shù) 類型 是否必填 最大長度 描述 示例值
app_id String 32 支付寶分配給開發(fā)者的應(yīng)用ID 2014072300007148
method String 128 接口名稱 alipay.system.oauth.token
format String 40 僅支持JSON JSON
charset String 10 請求使用的編碼格式,如utf-8,gbk,gb2312等 utf-8
sign_type String 10 商戶生成簽名字符串所使用的簽名算法類型,目前支持RSA2和RSA,推薦使用RSA2 RSA2
sign String 344 商戶請求參數(shù)的簽名串,詳見簽名 詳見示例
timestamp String 19 發(fā)送請求的時間,格式"yyyy-MM-dd HH:mm:ss" 2014-07-24 03:07:50
version String 3 調(diào)用的接口版本,固定為:1.0 1.0
app_auth_token String 40 詳見應(yīng)用授權(quán)概述

請求參數(shù)

參數(shù) 類型 是否必填 最大長度 描述
grant_type String - 值為authorization_code時,代表用code換??;值為refresh_token時,代表用refresh_token換取
code String 可選 - 授權(quán)碼,用戶對應(yīng)用授權(quán)后得到。
refresh_token String 可選 - 刷刷新令牌,上次換取訪問令牌時得到。見出參的refresh_token字段

響應(yīng)參數(shù)

參數(shù) 類型 是否必填 最大長度 描述 示例值
user_id String 必填 16 支付寶用戶的唯一userId 2088102150477652
access_token String 必填 40 訪問令牌。通過該令牌調(diào)用需要授權(quán)類接口 20120823ac6ffaa4d2d84e7384bf983531473993
expires_in String 必填 16 訪問令牌的有效時間,單位是秒。 3600
refresh_token String 必填 40 刷新令牌。通過該令牌可以刷新access_token 20120823ac6ffdsdf2d84e7384bf983531473993
re_expires_in String 必填 16 刷新令牌的有效時間,單位是秒。 3600

請求示例

JAVA

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode("4b203fe6c11548bcabd8da5bb087a83b");
request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("調(diào)用成功");
} else {
System.out.println("調(diào)用失敗");
}

.NET

IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", "app_id", "merchant_private_key", "json", "1.0", "RSA2", "alipay_public_key", "GBK", false);
AlipaySystemOauthTokenRequest  request= new AlipaySystemOauthTokenRequest() ;
request.GrantType = "authorization_code";
request.Code = "4b203fe6c11548bcabd8da5bb087a83b";
request.RefreshToken = "201208134b203fe6c11548bcabd8da5bb087a83b";
AlipaySystemOauthTokenResponse response=client.execute(request);
Console.WriteLine(response.Body);

PHP

$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = 'your app_id';
$aop->rsaPrivateKey = '請?zhí)顚戦_發(fā)者私鑰去頭去尾去回車,一行字符串';
$aop->alipayrsaPublicKey='請?zhí)顚懼Ц秾毠€,一行字符串';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='GBK';
$aop->format='json';
$request = new AlipaySystemOauthTokenRequest ();
$request->setGrantType("authorization_code");
$request->setCode("4b203fe6c11548bcabd8da5bb087a83b");
$request->setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
$result = $aop->execute ( $request); 


$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode)&&$resultCode == 10000){
echo "成功";
} else {
echo "失敗";
}

HTTP請求源碼

https://openapi.alipay.com/gateway.do?timestamp=2013-01-01 08:08:08&method=alipay.system.oauth.token&app_id=4472&sign_type=RSA2&sign=ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE&version=1.0&grant_type=
  "authorization_code"
&code=
  "4b203fe6c11548bcabd8da5bb087a83b"
&refresh_token=
  "201208134b203fe6c11548bcabd8da5bb087a83b"


//為確保安全通信,需自行驗證響應(yīng)示例中的sign值是否為螞蟻金服所提供。

響應(yīng)示例

JSON 示例

{
    "alipay_system_oauth_token_response": {
        "user_id": "2088102150477652",
        "access_token": "20120823ac6ffaa4d2d84e7384bf983531473993",
        "expires_in": "3600",
        "refresh_token": "20120823ac6ffdsdf2d84e7384bf983531473993",
        "re_expires_in": "3600"
    }
}

XML 示例

<alipay_system_oauth_token_response>
<code>10000</code>
<msg>Success</msg>  <alipay_user_id>已廢棄,請勿使用</alipay_user_id>
  <user_id>2088102150477652</user_id>
  <access_token>20120823ac6ffaa4d2d84e7384bf983531473993</access_token>
  <expires_in>3600</expires_in>
  <refresh_token>20120823ac6ffdsdf2d84e7384bf983531473993</refresh_token>
  <re_expires_in>3600</re_expires_in>
  <auth_token_type>permanent</auth_token_type>
</alipay_system_oauth_token_response>

異常示例

JSON 示例

{
    "alipay_system_oauth_token_response": {
        "code": "20000",
        "msg": "Service Currently Unavailable",
        "sub_code": "isp.unknow-error",
        "sub_msg": "系統(tǒng)繁忙"
    },
    "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

業(yè)務(wù)錯誤碼

公共錯誤碼

錯誤碼 錯誤描述 解決方案
isv.grant-type-invalid grant_type參數(shù)不正確 grant_type必須是authorization_code、refresh_token二者之一 若傳入authorization_code為code換取令牌,若傳入refresh_token為刷新令牌
isv.code-invalid 授權(quán)碼(auth_code) 
錯誤、狀態(tài)不對或過期
使用有效的auth_code重新執(zhí)行令牌換取,或引導(dǎo)用戶重新授權(quán)
isv.refresh-token-invalid 刷新令牌(refresh_token)錯誤或狀態(tài)不對 使用有效的refresh_token重新執(zhí)行令牌刷新,或引導(dǎo)用戶重新授權(quán)
isv.refresh-token-time-out 刷新令牌(refresh_token)過期 使用有效的refresh_token重新執(zhí)行令牌刷新,或引導(dǎo)用戶重新授權(quán)
isv.refreshed-token-invalid 刷新出來的令牌無效 使用返回的刷新令牌再次刷新
isv.invalid-app-id 調(diào)用接口的應(yīng)用標(biāo)識(app_id)與令牌授權(quán)的應(yīng)用不相符 傳入正確的app_id和令牌,若開發(fā)者支付寶賬號名下有多個app_id,或者開發(fā)者管理多個歸屬于不同支付寶賬號的app_id,請注意不要混用不同app_id的code
isp.unknow-error 未知錯誤 重試,或聯(lián)系支付寶客服
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號