使用淘宝api拼接url获取Access Token(sessionkey)|三体教程
文章目录

使用淘宝api拼接url获取Access Token(sessionkey)

今天要讲的是使用taobao.top.auth.token.create接口,来拼接授权url获得Access Token。

先拼接如下URL:

https://oauth.taobao.com/authorize?response_type=code&client_id=123456&redirect_uri=http://www.baidu.com/&state=1212&view=web

你可能会问如何进入这个网址,别想那么多,直接超链接过去,或是一个button跳转过去都可以。

其中client_id、response_type、redirect_uri是必填的。

client_id:等同于appkey

response_type:值为code

redirect_uri:回调地址,一定要是阿里开放平台中设置的回调URL。

使用淘宝api拼接url获取Access Token(sessionkey)

授权登录后,会跳转到。

http://www.baidu.com/?code=M61H6cqaqAd9FQadYycKr4MJ1126149&state=1212

网址中有code,我们就获取了对应的code,然后使用下面代码

$c = new TopClient;
$c->appkey = appkey;
$c->secretKey = secret;
$req = new TopAuthTokenCreateRequest;
$req->setCode("M61H6cqaqAd9FQadYycKr4MJ1126149");
$resp = $c->execute($req);

将获得json数据,如下:


$str='{"top_auth_token_create_response":{"token_result":"{"taobao_user_nick":"%E5%BA%97","re_expires_in":0,"expires_in":7776000,"expire_time":1529913724097,"r1_expires_in":7776000,"w2_valid":1529913724097,"taobao_user_id":"3256537496","w2_expires_in":7776000,"r1_valid":1529913724097,"w1_expires_in":7776000,"r2_valid":1529913724097,"w1_valid":1529913724097,"token_type":"Bearer","r2_expires_in":7776000,"refresh_token":"6201411ba3c7837d087ZZ2369ac80d43e3155d8b65322a03256537496","refresh_token_valid_time":1522137724097,"access_token":"6202911dd839584356dfh06f73ccdf8891b17b3f64750ce3256537496"}","request_id":"16swi3rvu6mqm"}}';


然后将其转换成数组,就可以很直观的看到数据了。

其中就有我们需要的access_token。

Array
(
    [taobao_user_nick] => %E5%BA%97
    [re_expires_in] => 0
    [expires_in] => 7776000
    [expire_time] => 1529913724097
    [r1_expires_in] => 7776000
    [w2_valid] => 1529913724097
    [taobao_user_id] => 3256537496
    [w2_expires_in] => 7776000
    [r1_valid] => 1529913724097
    [w1_expires_in] => 7776000
    [r2_valid] => 1529913724097
    [w1_valid] => 1529913724097
    [token_type] => Bearer
    [r2_expires_in] => 7776000
    [refresh_token] => 6201411ba3c7837d087ZZ2369ac80d43e3155d8b65322a03256537496
    [refresh_token_valid_time] => 1522137724097
    [access_token] => 6202911dd839584356dfh06f73ccdf8891b17b3f64750ce3256537496
)

当然你还可以先到“淘宝开放平台API测试工具”去测试一下,网址http://open.taobao.com/apitools/apiTools.htm

本文网址:http://www.santii.com/article/210.html

  • 1楼 [2018-04-09]请问获取到code之后又该如何操作呢?
    • 2楼 [2018-04-10]将获取到的code结合taobao.top.auth.token.create接口就可以获取到SESSIONKEY了。
      最后面有写的哦!


数据更新时间:2018-04-22