绑定QQ登录 PHP OAuth详解(转)
越来越多的网站开始采用 第三方账号登录 如qq 微博 开心网 等诸多流行的社交平台,利用第三方账号登录,可以更好地省去用户的注册时间,并且极大地提高用户体验,吸引更多的潜在用户。
让用户拥有 一个号码走web天下的感觉。。。,对我们开发者来说 这也是一个主要的学习新知识点 主要原理即Oauth。
<span style="font-family: SimHei, 黑体, tahoma, arial, helvetica, sans-serif;">OAUTH协议为用户资源的授权提供了一个安全的、开放而又简易的标准。与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用户的用户名与密码就可以申请获得该用户资源的授权,因此OAUTH是安全的。</span>
步骤详细介绍
1.去qq、微博的第三方开发平台 去review一下他们的API
2.申请AppId和AppKey ,对于qq来说 网址 http://connect.opensns.qq.com/ 点击首页的 <网站接入>
注册自己的开发者信息 随便百度一下 输入一个备案号 即可 然后我们就可以获得 AppId和AppKey
<span style="font-family: SimHei, 黑体, tahoma, arial, helvetica, sans-serif;"><?php
/**
* 申请http://connect.opensns.qq.com/apply
* 列表http://connect.opensns.qq.com/my
*/
session_start();
$qq_oauth_config = array(
'oauth_consumer_key'=>'*******',//APP ID
'oauth_consumer_secret'=>'******************',//APP KEY
'oauth_callback'=>"http://www.955.cc/qq.php?action=reg",//这里修改为当前脚本
'oauth_request_token_url'=>"http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token",
'oauth_authorize_url'=>'http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize',
'oauth_request_access_token_url'=>'http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token',
'user_info_url' => 'http://openapi.qzone.qq.com/user/get_user_info',
);
$action = isset($_GET['action']) ? $_GET['action'] : ''; $qq = new qq_oauth($qq_oauth_config);
switch($action){
//用户登录 Step1:请求临时token
case 'login':
$token = $qq->oauth_request_token();
$_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
$qq->authorize($token['oauth_token']);
break;
//Step4:Qzone引导用户跳转到第三方应用
case 'reg':
$qq->register_user();
$access_token = $qq->request_access_token();
if($token = $qq->save_access_token($access_token)){
//保存,一般发给用户cookie,以及用户入库
//var_dump($token);
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
$_SESSION['openid'] = $token['openid'];
header('Content-Type: text/html; charset=utf-8');
$user_info = json_decode($qq->get_user_info());
if($user_info->ret!=0){
exit("获取头像昵称时发生错误".$user_info->msg);
} else {
echo 'QQ昵称:',$user_info->nickname,
'<img src="',$user_info->figureurl,'" />',
'<img src="',$user_info->figureurl_1,'" />',
'<img src="',$user_info->figureurl_2,'" />';
} }
break;
default :
} class qq_oauth{
private $config;
function __construct($config){
$this->config = $config;
}
/**
* 返回配置
* @param string $name
*
*/
function C($name){
return isset($this->config[$name]) ? $this->config[$name] : FALSE;
}
/**
* 构建请求URL
* @param string $url
* @param array $params
* @param string $oauth_token_secret
*
*/
function build_request_uri($url,$params=array(),$oauth_token_secret=''){
$oauth_consumer_key = $this->C('oauth_consumer_key');
$oauth_consumer_secret = $this->C('oauth_consumer_secret'); $params = array_merge(array(
'oauth_version'=>'1.0',
'oauth_signature_method'=>'HMAC-SHA1',
'oauth_timestamp'=>time(),
'oauth_nonce'=>rand(1000,99999999),
'oauth_consumer_key'=>$oauth_consumer_key,
),$params);
$encode_params = $params;
ksort($encode_params);
$oauth_signature = 'GET&'.urlencode($url).'&'.urlencode(http_build_query($encode_params));
$oauth_signature = base64_encode(hash_hmac('sha1',$oauth_signature,$oauth_consumer_secret.'&'.$oauth_token_secret,true));
$params['oauth_signature'] = $oauth_signature;
return $url.'?'.http_build_query($params);
}
/**
* 校验回调是否返回约定的参数
*/
function check_callback(){
if(isset($_GET['oauth_token']))
if(isset($_GET['openid']))
if(isset($_GET['oauth_signature']))
if(isset($_GET['timestamp']))
if(isset($_GET['oauth_vericode']))
return true;
return false;
} function get_contents($url){
$curl = curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_URL,$url);
return curl_exec($curl);
}
/**
* Step1:请求临时token、Step2:生成未授权的临时token
*/
function oauth_request_token(){
$url = $this->build_request_uri($this->C('oauth_request_token_url'));
$tmp_oauth_token = $this->get_contents($url);
parse_str($tmp_oauth_token);
/*
oauth_token 未授权的临时token
oauth_token_secret token的密钥,该密钥仅限于临时token
error_code 错误码
*/
if(isset($error_code)) exit($error_code);
return array(
'oauth_token'=>$oauth_token,
'oauth_token_secret'=>$oauth_token_secret
);
}
/**
* Step3:引导用户到Qzone的登录页
* @param string $oauth_token 未授权的临时token
*/
function authorize($oauth_token){
$str = "HTTP/1.1 302 Found";
header($str);
$url = $this->C('oauth_authorize_url');
$query_strings = http_build_query(array(
'oauth_consumer_key'=>$this->C('oauth_consumer_key'),
'oauth_token'=>$oauth_token,
'oauth_callback'=>$this->C('oauth_callback'),
));
header('Location: '.$url.'?'.$query_strings);
}
/**
* Step4:Qzone引导用户跳转到第三方应用
* @return bool 验证是否有效
*/
function register_user(){
/*
* oauth_token 已授权的临时token
* openid 腾讯用户对外的统一ID,该OpenID与用户QQ号码一一对应
* oauth_signature 签名值,方便第三方来验证openid以及来源的可靠性。
* 使用HMAC-SHA1算法:
* 源串:openid+timestamp(串中间不要添加'+'符号)
* 密钥:oauth_consumer_secret
* timestamp openid的时间戳
* oauth_vericode 授权验证码。
*/
if($this->check_callback()){
//校验签名
$signature = base64_encode(hash_hmac('sha1',$_GET['openid'].$_GET['timestamp'],$this->C('oauth_consumer_secret'),true));
if(!empty($_GET['oauth_signature']) && $signature==$_GET['oauth_signature']){
$_SESSION['oauth_token'] = $_GET['oauth_token'];
$_SESSION['oauth_vericode'] = $_GET['oauth_vericode'];
return;
}
}
//校验未通过
exit('UNKNOW REQUEST');
}
/**
* Step5:请求access token
*/
function request_access_token(){
$url = $this->build_request_uri($this->C('oauth_request_access_token_url'),array(
'oauth_token'=>$_SESSION['oauth_token'],
'oauth_vericode'=>$_SESSION['oauth_vericode']
),$_SESSION['oauth_token_secret']);
return $this->get_contents($url);
}
/**
* Step6:生成access token (保存access token)
*
* 关于access_token
* 目前access_token(及其secret)是长期有效的,和某一个openid对应,目前可以支持线下获取该openid的信息。
* 当然,用户有权限在Qzone这边删除对第三方的授权,此时该access_token会失效,需要重新走整个流程让用户授权。
* 以后会逐步丰富access_token的有效性,长期有效、短期有效、用户登录时才有效等。
*/
function save_access_token($access_token_str){
parse_str($access_token_str,$access_token_arr);
if(isset($access_token_arr['error_code'])){
return FALSE;
} else {
return $access_token_arr;
}
}
/**
* 目前腾讯仅开放该API
* 获取登录用户信息,目前可获取用户昵称及头像信息。
* http://openapi.qzone.qq.com/user/get_user_info
*/
function get_user_info(){
$url = $this->build_request_uri($this->C('user_info_url'),array(
'oauth_token'=>$_SESSION['oauth_token'],
'openid'=>$_SESSION['openid'],
),$_SESSION['oauth_token_secret']);
return $this->get_contents($url);
}
}
</span>
转载自:http://www.iteye.com/topic/1129210
绑定QQ登录 PHP OAuth详解(转)的更多相关文章
- [转帖]SSH远程登录配置文件sshd_config详解
SSH远程登录配置文件sshd_config详解 2016年06月02日 17:42:25 Field_Yang 阅读数 61386 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权 ...
- SSH 证书登录(实例详解)
SSH 证书登录(实例详解) 客户端通过私钥登录 ssh 服务器 CentOS 7 SSH 使用证书登录 使用私钥 ssh 登陆 CentOS
- sql server 2008 数据库管理系统使用SQL语句创建登录用户步骤详解
介绍了sql server 2008 数据库管理系统使用SQL语句创建登录用户步骤详解 --服务器角色: --固定服务器角色具有一组固定的权限,并且适用于整个服务器范围. 它们专门用于管理 SQL S ...
- Shiro安全框架入门篇(登录验证实例详解与源码)
转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...
- Citrix XenApp登录服务器过程详解
详细流程: 1. 客户端上的receiver负责解析ICA文件,并根据ICA文件的内容发起连接请求.若是外网访问,则ICA文件中记录的是NetScaler的AG FQDN信息,连接请求发至NetSca ...
- oracle数据库启动流程及登录认证方式详解
转自:https://www.2cto.com/database/201803/726644.html ■ oracle启动流程-windows下 1) lsnrctl start (启动监听) ...
- SSH免密码登录配置方法详解
1.测试主机配置信息: 192.168.100.236 db06.chavin.king db06 192.168.100.237 db07.chavin.king db07 2.创建测试用户: gr ...
- SSH入门开发(实现一个简单的登录功能)详解
开头,首先想记录下一首诗,是今天拇指阅读看到的:很有感触,所以乐于分享: 那么,下面正式开始进入正题,搭建一个 SSH完整的项目: 首先,我们需要在WEB-ROOT下创建一个login.jsp(登录) ...
- 【ZZ】详解哈希表的查找
详解哈希表的查找 https://mp.weixin.qq.com/s/j2j9gS62L-mmOH4p89OTKQ 详解哈希表的查找 2018-03-01 算法与数据结构 来自:静默虚空 http: ...
随机推荐
- GCC高级测试功能扩展——程序性能测试工具gprof、程序覆盖测试工具gcov
gprof是GNU组织下的一个比较有用的性能测试功能: 主要功能: 找出应用程序中消耗CPU时间最多的函数: 产生程序运行时的函数调用关系.调用次数 基本原理: 首先用户要使用gprof工具, ...
- myeclipse报异常:java.lang.OutOfMemoryError: Java heap space
- python发送QQ邮箱方法
import smtplib from email.mime.text import MIMEText mail_user = "user1@qq.com" mail_pwd = ...
- MySQL 官方文档
MySQL 5.6 Reference Manual Preface and Legal Notices 1 General Information 2 Installing and Upgradin ...
- Mac安装Myeclipse2015开发环境
1.下载Myeclipse2015 链接: http://pan.baidu.com/s/1jHe8mFk 密码: qgeb 下载下来后,在安装的时候需要自己设置下安装目标,不然在破解的时候不是太好找 ...
- Android Bluetooth模块学习笔记
一.蓝牙基础知识 1.蓝牙( Bluetooth )是一种无线技术标准,可实现固定设备.移动设备和楼宇个人域网之间的短距离数据交换.蓝牙基于设备低成本的收发器芯片,传输距离近.低功耗. 2.微波频段: ...
- 【BIRT】修改BIRT的背景颜色
修改BIRT报表的背景颜色都在这里了 在BIRT的webcontent/birt/styles/目录下有如下文件列表: dialogbase.css文件修改 dialogbase_rtl.css文件修 ...
- 初学者必知的Python中优雅的用法
转自:http://python.jobbole.com/81393/
- 【PHP】在目标字符串指定位置插入字符串
PHP如何在指定位置插入相关字符串,例子:123456789变为1_23_456789插入"_"到指定的位置! (可以用作换行或者其他处理) 插入示例,具体思路在代码中有注释: & ...
- ssh(安全外壳协议)
SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专 ...