通过api获取到淘宝接口的access_token

第一步:

//获取授权code
public function ajaxGetAuth(){
$this->redirect('https://oauth.taobao.com/authorize?response_type=code&client_id=27703326&redirect_uri=http://plugin.heifeng.xin/index.php/Mange/Mange/index&state=1212&view=web');
}

redirect_uri=http://plugin.heifeng.xin/index.php/Mange/Mange/index是回调地址

client_id=27703326 是接口的appkey

第二步:回调地址处理的数据,获取access_token

//回调的方法

// 获取accesstoken
public function index(){
    $params = input();
    if(isset($params['code'])){
        $url = 'https://eco.taobao.com/router/rest';
        $params_list = array(
            'method' =>'taobao.top.auth.token.create', //Api方法
            'app_key' =>'27703326', // TOP分配给应用的AppKey
            'timestamp' =>date('Y-m-d H:i:s',time()), //时间戳,格式为yyyy-MM-dd HH:mm:ss,时区为GMT+8,例如:2015-01-01              12:00:00。淘宝API服务端允许客户端请求最大时间误差为10分钟
            'format' =>'json', //响应格式。默认为xml格式,可选值:xml,json。
            'v' =>'2.0', //API协议版本,可选值:2.0
            'sign_method' =>'md5', // API输入参数签名结果,签名算法参照下面的介绍。
            'client_id' =>'27703326', // 等同于appkey,创建应用时获得。
            'client_secret' =>'c96813d5dff4624671fcd36aa3338446', //等同于AppSecret,创建应用时获得。
            'grant_type' =>'authorization_code', //授权类型 ,值为authorization_code
            'code' =>$params['code'], //上一步获取code得到
            'redirect_uri' =>'http://plugin.heifeng.xin/index.php/Mange/Mange/receiveSession', //可填写应用注册时回调地址域                        名.redirect_uri指的是应用发起请求时,所传的回调地址参数,在用户授权后应用会跳转至redirect_uri。要求与应用注册              时填写的回调地址域名一致或顶级域名一致 。
            'state' =>'1212', //可自定义,如1212等;维持应用的状态,传入值与返回值保持一致
            'view' =>'web', //可选web、tmall或wap其中一种,Web对应PC端(淘宝logo)浏览器页面样式;Tmall对应天猫的浏览器              页面样式;Wap对应无线端的浏览器页面样式。
        );
        $sign = $this->testTwo($params_list); //获取签名
        $params_list['sign'] = $sign;
        $data = request_post($url, $params_list);
        $data = json_decode($data,true);
        $data = json_decode($data['top_auth_token_create_response']['token_result'],true);
        if($data){
            foreach ($data as $key => $value) {
                $data[$key] = urldecode($value);
            }
        }
        $access_token = $data['access_token']; //Access token
        $refresh_token = $data['refresh_token']; //Refresh token,可用来刷新access_token
        $this->receiveSession($access_token,$refresh_token); //回调处理函数
    }
    return null;
}
//签名算法
public function testTwo($params_list){
    ksort($params_list);
    $param_link = null;
    foreach ($params_list as $key => $value) {
        $param_link .= $key.$value;
    }
    $param_link = 'c96813d5dff4624671fcd36aa3338446'.$param_link.'c96813d5dff4624671fcd36aa3338446';
    $param_link = utf8_encode($param_link);
    $md5_secret = strtoupper(md5($param_link));
    return $md5_secret;
}

public function receiveSession(){
    dump(111);die;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值