阿里巴巴开放平台Oauth2.0协议获取access_token

如果对Oauth2不太熟,有时间的话可以去学习一下之前写的一篇

Oauth2.0 oauth2-server-php的使用Demo,怎么连接redis/可实现thinkphp5/yii/Laravel中使用

阿里巴巴composer 包:

阿里巴巴开放平台SDK

一、获取Code

//获取的code有效时间2分钟

    public function getCode(){    

        #拼接获取Code的URL

        $url='http://gw.api.alibaba.com/openapi';

        $appKey='';

        $appSecret ='';    

        #回调URL

        $redirectUrl = 'http://127.0.0.1/auth.php';

        #生成签名

        $code_arr = array(

            'client_id' => $appKey,

            'redirect_uri' => $redirectUrl,

            'site' => '1688'

        );

        ksort($code_arr);

        $sign_str = '';

        foreach ($code_arr as $key=>$val){

            $sign_str .= $key . $val;

        }

        $code_sign = strtoupper(bin2hex(hash_hmac("sha1", $sign_str, $appSecret, true)));

        $get_code_url = 'http://gw.api.alibaba.com/auth/authorize.htm?client_id='.$appKey.'&site=aliexpress&redirect_uri='.$redirectUrl.'&_aop_signature='.$code_sign;

        $get_code_url;    

    }

二、拿Code 换access_token

public function gettoken(){

        #根据code获取refresh_token、access_token 

        $url = 'http://gw.api.alibaba.com/openapi';

        $appKey = '';

        $appSecret ='';

        $redirectUrl =  'http://http://127.0.0.1/auth.php';

        #此处code即为上面的方法getCode取得的code值

        $code='';



        #拼接获取token的Url

        $getTokenUrl='https://gw.api.alibaba.com/openapi/http/1/system.oauth2/getToken/'.$appKey;

        $data='grant_type=authorization_code&need_refresh_token=true&client_id='.$appKey.'&client_secret='.$appSecret.'&redirect_uri='.$redirectUrl.'&code='.$code;

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $getTokenUrl);

        curl_setopt($ch, CURLOPT_POST, true);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $result = curl_exec($ch);

        curl_close($ch);

        #打印出返回结果

        print_r(json_decode($result));

        exit;

    }

和我做朋友?

廖圣平博客整理

参考:www.04007.cn

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
阿里巴巴开放平台 SDK 运行样例。 调用阿里巴巴开放平台的API你是不是获取不到访问口令,看一下我的例子吧,原因是后台已经更改,而网站的说明没有更新。 IniFile ini = new IniFile(); Dictionary sl = new Dictionary(); sl = ini.GetSectionValues("Setting"); strAppKey = sl["appKey"].ToString(); strAccToken = sl["access_token"].ToString(); strAppSecret = sl["appSecret"].ToString(); string strTokeyTime = sl["datatime"].ToString(); string strRefresh_token = sl["refresh_token"].ToString(); //比较令牌保存时间,如果比现在早10个小时以上就更新 DateTime dt = Convert.ToDateTime(strTokeyTime); TimeSpan ts = System.DateTime.Now.Subtract(dt); if ((Int16.Parse(ts.Days.ToString()) >= 1) || (decimal.Parse(ts.Hours.ToString()) > 8)) { //超过有效期,重新获取Access_Token //利用Refresh_token获取access_token Dictionary ls = new Dictionary(); string urlStr = "https://gw.open.china.alibaba.com/openapi/http/1/system.oauth2/getToken/" + strAppKey; Dictionary dc = new Dictionary(); dc.Add("grant_type", "refresh_token"); dc.Add("need_refresh_token", "true"); dc.Add("client_id", strAppKey); dc.Add("client_secret", strAppSecret); dc.Add("redirect_uri", "http://localhost"); dc.Add("refresh_token", strRefresh_token); WebUtils wu = new WebUtils(); string tbxToken = wu.DoPost(urlStr, dc); Hashtable hs = (Hashtable)PluSoft.Utils.JSON.Decode(tbxToken); //保存令牌 ini.WriteValue("Setting", "access_token", hs["access_token"].ToString()); ini.WriteValue("Setting", "datatime", System.DateTime.Now.ToString()); strAccToken = hs["access_token"].ToString(); } 获取授权的CODE和令牌,分两步。 一、获取CODE   将得到的CODE码复制到文本框中: 二、得到令牌,并存在本地   三、利用refresh_token获取access_token 四、两个例子: [Setting] appKey=输入你的App ID appSecret=输入你的App Secret Key refresh_token=f3863b17-dcf3-45f6-8787-481befb188c7 access_token=faf4a195-e87d-4bfd-afbd-dc804c264c9c datatime=2012-12-2 16:53:33 [tu] albumId=35150663 albumName=产品相册J imageCount=90 本Demo为接入阿里平台的“HelloWorld”,主要调用平台的免登录接口接入平台。Demo中对平台提供的SIP接 口返回值进行解析,其中解析和签名部分适用于平台上提供的所有REST风格接口。 一、线上Demo运行效果查看 本Demo已经部署到线上环境,您可以试订购,订购后在“我的软件”中将有此Demo入口,点击使用即可。 Demo订购URL:http://mall.alisoft.com/apps/shopwindow/showAppDetailAction!view.jspa?appID=16857 二、Demo 结构介绍 1、本Demo采用Visual Studio2005开发的web网站项目。 2、Demo中主要文件介绍: Default.aspx,显示免登录是否成功页面,显示调用免登录系统级参数和应用级参数。 Signature.cs ,签名参数生成工具类,其中code为软件注册后获得的CERT CODE。 三、本地部署 1.确认安装有Visual Studio或IIS,安装.net Framawork。 2.将项目导入Visual Studio或部署到IIS服务器,或者将项目拷贝到D盘,点击dotnethelloworld.sln打开项目。 3.配置CERT CODE Signature.cs中将code设置成自己注册软件的CERT CODE,因为签名会用到此安全编码。 四、运行 1.软件入口: 线上环境demo入口为:http://demo.aliapp.com/dotnethelloworld/Default.aspx 本地部署入口如为:http://localhost:1702/dotnethelloworld/Default.aspx 2.将软件入口录入到您注册软件的“软件测试入口”。 3.点击阿里软件集市平台中“我的软件”,找到自己部署软件,点击使用即可登录您本地的应用。 也可在开发者工作台中点击“免登录接口测试”,进入您刚部署的Demo应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廖圣平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值