建立淘寶開放平臺應用的步驟簡單記錄
阿新 • • 發佈:2019-01-28
記錄一下淘寶開放平臺應用開發的步驟。
1,註冊成為開發者,建立一個應用,
參照下面的連結如何選擇應用
http://open.taobao.com/doc/detail.htm?id=101710
建立應用的時候有一個回撥地址url,
回撥地址是用來接收TOP(開放平臺)返回授權相關資料的,
下面是你的應用使用TOP API的流程:在沒有上線之前測試都在所謂的沙箱模式下進行的。
1,以web應用為例,使用者訪問你的web,你的web應用redirect使用者到淘寶的登入認證,比如如下
https://oauth.tbsandbox.com/authorize?response_type=code&client_id=1021738064&redirect_uri=http%3A%2F%2Fdev2dev.sinaapp.com%2Ftaobao%2Fcallback.php&from_site=fuwu
2,使用者用淘寶賬號登入,redirec使用者到回撥的url,就是你的應用的一個url
3,一個授權碼code會作為引數傳給回撥的url,你的應用用這個授權碼code以post的方式訪問淘寶的
https://oauth.tbsandbox.com/token獲取token
4,得到token後就可以呼叫淘寶的API了。
參照下面的連結獲取更多關於認證的資訊
http://open.taobao.com/doc/detail.htm?spm=0.0.0.0.CSGRVZ&id=118
一下注意點:
1,在測試的時候使用的是沙箱環境的AppKey和AppSecret,淘寶端認證的url也是沙箱環境的tbsandbox。
2,用的登入賬戶也必須是沙箱環境的,http://www.tbsandbox.com/doc/index.html#taobao_acount
3,淘寶提供的呼叫示例http://open.taobao.com/doc/detail.htm?spm=0.0.0.0.mtTHeu&id=131 'session_key' => $sessionkey,應該改為'session' => $sessionkey,
下面是回撥頁面的程式碼:
//$url = 'https://oauth.taobao.com/token'; //prod env
echo curl($url,$paramArr); //顯示返回資訊echo "$code";?> 下面是應用呼叫淘寶API的程式碼:利用回撥頁面獲取的session tokentest.php
util.php
同時認證後傳給應用的回撥url的引數是$code = $_GET["top_session"];//prod enc
淘寶的使用者認證url也應該換成正式環境的
http://container.api.taobao.com/container?appkey={appkey}
淘寶的token認證url也應該換成正式環境的
$url = 'https://oauth.taobao.com/token'; //prod env
API呼叫url換成如下
1,註冊成為開發者,建立一個應用,
參照下面的連結如何選擇應用
http://open.taobao.com/doc/detail.htm?id=101710
建立應用的時候有一個回撥地址url,
回撥地址是用來接收TOP(開放平臺)返回授權相關資料的,
下面是你的應用使用TOP API的流程:在沒有上線之前測試都在所謂的沙箱模式下進行的。
1,以web應用為例,使用者訪問你的web,你的web應用redirect使用者到淘寶的登入認證,比如如下
https://oauth.tbsandbox.com/authorize?response_type=code&client_id=1021738064&redirect_uri=http%3A%2F%2Fdev2dev.sinaapp.com%2Ftaobao%2Fcallback.php&from_site=fuwu
2,使用者用淘寶賬號登入,redirec使用者到回撥的url,就是你的應用的一個url
3,一個授權碼code會作為引數傳給回撥的url,你的應用用這個授權碼code以post的方式訪問淘寶的
https://oauth.tbsandbox.com/token獲取token
4,得到token後就可以呼叫淘寶的API了。
參照下面的連結獲取更多關於認證的資訊
http://open.taobao.com/doc/detail.htm?spm=0.0.0.0.CSGRVZ&id=118
一下注意點:
1,在測試的時候使用的是沙箱環境的AppKey和AppSecret,淘寶端認證的url也是沙箱環境的tbsandbox。
2,用的登入賬戶也必須是沙箱環境的,http://www.tbsandbox.com/doc/index.html#taobao_acount
3,淘寶提供的呼叫示例http://open.taobao.com/doc/detail.htm?spm=0.0.0.0.mtTHeu&id=131 'session_key' => $sessionkey,應該改為'session' => $sessionkey,
下面是回撥頁面的程式碼:
<?php function curl($url, $postFields = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (is_array($postFields) && 0 < count($postFields)) { $postBodyString = ""; $postMultipart = false; foreach ($postFields as $k => $v) { if("@" != substr($v, 0, 1))//判斷是不是檔案上傳 { $postBodyString .= "$k=" . urlencode($v) . "&"; } else//檔案上傳用multipart/form-data,否則用www-form-urlencoded { $postMultipart = true; } } unset($k, $v); curl_setopt($ch, CURLOPT_POST, true); if ($postMultipart) { curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); } } $reponse = curl_exec($ch); if (curl_errno($ch)) { throw new Exception(curl_error($ch),0); } else { $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (200 !== $httpStatusCode) { throw new Exception($reponse,$httpStatusCode); } } curl_close($ch); return $reponse; } header("Content-Type:text/html;charset=UTF-8"); require_once 'util.php'; $appKey = '沙箱環境的appkey'; $appSecret = '沙箱環境的appSecret '; //$sessionkey= 'test'; $code = $_GET["code"];
//$code = $_GET["top_session"];//prod enc
$paramArr = array( 'code' => $code, 'grant_type' => "authorization_code", 'client_id' => $appKey, 'client_secret' => $appSecret, 'redirect_uri' => "http://dev2dev.sinaapp.com/taobao/test.php");//引數陣列$url = 'https://oauth.tbsandbox.com/token';//$url = 'https://oauth.taobao.com/token'; //prod env
echo curl($url,$paramArr); //顯示返回資訊echo "$code";?> 下面是應用呼叫淘寶API的程式碼:利用回撥頁面獲取的session tokentest.php
<?php header("Content-Type:text/html;charset=UTF-8"); require_once 'util.php'; $appKey = '沙箱的appkey'; $appSecret = '沙箱的appsecret'; $sessionkey= '6202a275b902c321c324db810764ecbae6ace39da3f702f2074082787';//回撥頁面得到code後再獲取的session token //引數陣列 $paramArr = array( 'app_key' => $appKey, 'session' => $sessionkey, 'method' => 'taobao.user.seller.get', 'format' => 'json', 'v' => '2.0', 'sign_method'=>'md5', 'timestamp' => date('Y-m-d H:i:s'), 'fields' => 'user_id,nick,sex,seller_credit,type,has_more_pic,item_img_num,item_img_size,prop_img_num,prop_img_size,auto_repost,promoted_type,status,alipay_bind,consumer_protection,avatar,liangpin,sign_food_seller_promise,has_shop,is_lightning_consignment,has_sub_stock,is_golden_seller,vip_info,magazine_subscribe,vertical_market,online_gaming' ); //生成簽名 $sign = createSign($paramArr); //組織引數 $strParam = createStrParam($paramArr); $strParam .= 'sign='.$sign; //訪問服務 $url = 'http://gw.api.tbsandbox.com/router/rest?'.$strParam; //沙箱環境呼叫地址 //$url = 'http://gw.api.taobao.com/router/rest?'.$strParam; //prod呼叫地址 $result = file_get_contents($url); $result = json_decode($result); echo "json的結構為:"; print_r($result); echo "<br>"; echo "使用者名稱稱為:".$result->user_get_response->user->nick; echo "<br>"; echo "買家信用等級為:".$result->user_get_response->user->buyer_credit->level; ?>
util.php
如果想用正式環境測試,需要把appKey,appSecret換成正式環境的。<?php //簽名函式 function createSign ($paramArr) { global $appSecret; $sign = $appSecret; ksort($paramArr); foreach ($paramArr as $key => $val) { if ($key != '' && $val != '') { $sign .= $key.$val; } } $sign.=$appSecret; $sign = strtoupper(md5($sign)); return $sign; } //組參函式 function createStrParam ($paramArr) { $strParam = ''; foreach ($paramArr as $key => $val) { if ($key != '' && $val != '') { $strParam .= $key.'='.urlencode($val).'&'; } } return $strParam; } ?>
同時認證後傳給應用的回撥url的引數是$code = $_GET["top_session"];//prod enc
淘寶的使用者認證url也應該換成正式環境的
http://container.api.taobao.com/container?appkey={appkey}
淘寶的token認證url也應該換成正式環境的
$url = 'https://oauth.taobao.com/token'; //prod env
API呼叫url換成如下
$url = 'http://gw.api.taobao.com/router/rest?'.$strParam; //prod呼叫地址
使用者認證入口
<div id="container">
<a href="https://oauth.tbsandbox.com/authorize?response_type=code&client_id=xxxx&redirect_uri=http%3A%2F%2Fdev2dev.sinaapp.com%2Ftaobao%2Fcallback.php&from_site=fuwu">login sandbox</a>
<br>
<a href="http://container.api.taobao.com/container?appkey=yyy">login prod</a>
</div>