淘宝:淘宝沙箱测试环境--操作帮助
淘宝:淘宝沙箱测试环境-创建淘宝开放平台应用的步骤简单记录
http://www.tbsandbox.com/doc/index.html#account
记录一下淘宝开放平台应用开发的步骤。
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"];
- <pre code_snippet_id="195967" snippet_file_name="blog_20140220_1_5824846" name="code" class="php">//$code = $_GET["top_session"]; //prod enc</pre>$paramArr = array( 'code' =& gt; $code, 'grant_type' => "authorization_code", 'client_id' =& gt; $appKey, 'client_secret' => $appSecret, 'redirect_uri' =& gt; "http://dev2dev.sinaapp.com/taobao/test.php");//参数数组$url = 'https: //oauth.tbsandbox.com/token';<br>
- //$url = 'https://oauth.taobao.com/token'; //prod env<br>
- echo curl($url,$paramArr); //显示返回信息echo "$code";?>
- <pre></pre>
- 下面是应用调用淘宝API的代码:利用回调页面获取的session tokentest.php<pre code_snippet_id="195967" snippet_file_name="blog_20140220_2_186672" name="code" class="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;
- ?></pre><br>
- util.php
- <p></p>
- <p></p>
- <pre code_snippet_id="195967" snippet_file_name="blog_20140220_3_4439814" name="code" class="php"><?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;
- }
- ?></pre>如果想用正式环境测试,需要把appKey,appSecret换成正式环境的。<br>
- 同时认证后传给应用的回调url的参数是$code = $_GET["top_session"];//prod enc<br>
- 淘宝的用户认证url也应该换成正式环境的<br>
- http://container.api.taobao.com/container?appkey={appkey}<br>
- 淘宝的token认证url也应该换成正式环境的<br>
- $url = 'https://oauth.taobao.com/token'; //prod env<br>
- API调用url换成如下<br>
- <p>$url = 'http://gw.api.taobao.com/router/rest?'.$strParam; //prod调用地址</p>
- <p>用户认证入口</p>
- <p><div id="<a href="http://lib.csdn.net /base/4" class="replace_word" title="Docker知识 库" target="_blank" style="color:#df3434; font-weight:bold;">Container</a>"><br>
- <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>
- <br><br>
- <a href="http://container.api.taobao.com/container?appkey=yyy">login prod</a><br>
- </div></p>
- <p><br>
- </p>
- <p></p>
- <pre></pre>