官方api地址:http://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0
1.去qq互联上注册申请成为开发者,并创建一个应用获得appid,appkey
2.在需要QQ登录的页面加上qq小图标,并在图标外层a标签上加上 onclick='tologin()' 事件,底部加上script
1 <script> 2 function toLogin(){ 3 //以下为按钮点击事件的逻辑。注意这里要重新打开窗口 4 //否则后面跳转到QQ登录,授权页面时会直接缩小当前浏览器的窗口,而不是打开新窗口 5 var A=window.open("/ext/qq_login/index.php","TencentLogin","width=450,height=320,menubar=0,scrollbars=1,resizable=1,status=1,titlebar=0,toolbar=0,location=1"); 6 } 7 </script>
3.下载sdk 官网下载地址:http://wiki.connect.qq.com/sdk%E4%B8%8B%E8%BD%BD
a.下载了sdk后,将sdk放到项目中,例如当前放在了根目录,sdk命名为qq_login
b.浏览器访问 yourdomain/qq_login/install/,进行安装配置
qq互联上配置应用的callback为 yourdomain/qq_login/callback.php,这里安装的配置也应该填 yourdomain/qq_login/callback.php
c.重命名或删除掉install
d.修改sdk根目录index.php 内容为
1 <?php 2 require_once("./API/qqConnectAPI.php"); 3 $qc = new QC(); 4 $qc->qq_login();
e.在sdk根目录即index.php同级目录建一个文件叫callback.php,callback.php即成功调用qq登录后执行的操作
4.登录成功,访问callback.php文件
登录成功时会跳转到你配置的callback中,url上会带有一个get请求的code参数
a.接收code参数,利用code参数和你的appid,appkey获取access_token
b.获取到的access_token即为用户唯一标识符,你就可以用来注册到自己数据库中了
1 <?php 2 session_start(); 3 /** 4 * Step1:获取Authorization Code 5 * Step2:通过Authorization Code获取Access Token 6 * author: porter 7 */ 8 define("HOST_INFO",'http://'.$_SERVER['HTTP_HOST']); 9 $API_ID = "xx"; 10 $API_KEY = "xx"; 11 $code = $_GET['code']; 12 // 根据code获取access_token 13 // grant_type 必须 授权类型,在本步骤中,此值为“authorization_code”。 14 // client_id 必须 申请QQ登录成功后,分配给网站的appid。 15 // client_secret 必须 申请QQ登录成功后,分配给网站的appkey。 16 // code 必须 上一步返回的authorization code。 17 // 如果用户成功登录并授权,则会跳转到指定的回调地址,并在URL中带上Authorization Code。 18 // 例如,回调地址为www.qq.com/my.php,则跳转到: 19 // http://www.qq.com/my.php?code=520DD95263C1CFEA087****** 20 // 注意此code会在10分钟内过期。 21 // redirect_uri 必须 与上面一步中传入的redirect_uri保持一致。 22 $url = "https://graph.qq.com/oauth2.0/token"; 23 $url .= "?grant_type=authorization_code"; 24 $url .= "&client_id=$API_ID"; 25 $url .= "&client_secret=$API_KEY"; 26 $url .= "&code=$code"; 27 $url .= "&redirect_uri=http://test.www.xx.com/ext/qq_login/callback.php"; 28 $resultStr = file_get_contents($url); 29 // 解析返回的数据 30 $resultArr = explode('&' , $resultStr); 31 $result = array(); 32 foreach($resultArr as &$v){ 33 list($key , $value) = explode('=' , $v); 34 $result[$key] = $value; 35 } 36 // 将返回结果中的access_token进行登录操作 37 $data['uuid'] = $result['access_token']; 38 $data['type'] = 'qq'; 39 $data['s'] = 'asdf'; //用户模拟手机请求 40 $ch = curl_init (); // 启动一个CURL会话 41 curl_setopt($ch, CURLOPT_URL, HOST_INFO."/user/tlogin.html"); 42 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 43 // post数据 44 curl_setopt($ch, CURLOPT_POST, 1); 45 // post的变量 46 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 47 $return = curl_exec($ch); 48 curl_close($ch); 49 // 登录成功记录session 50 $return = json_decode($return , true); 51 if($return['status'] == 1){ 52 $_SESSION['user'] = $return['data']; 53 } 54 header("Location:".HOST_INFO); 55 ?>
可能出现的问题:
QQ登录时提示"redirect *** 100010"这样的:
你qq互联配置的callback与项目的callback不一样
在callback.php文件中登录成功保存了session,用header方法跳转页面,session无法传递:
在callback.php第一行写上 session_start();