网站应用接入qq登陆

想使用

话不多说,直接上代码

第一步 单击登录按钮进行页面跳转(需要修改/oauth/qq/index.php这个链接,换成自己的授权的页面)

<?php

session_start();

if($_SESSION['qqInfo'])

{

    echo '<pre>';

    print_r($_SESSION['qqInfo']);

}else{

    $str = '

    <html lang="zh-cn">

        <head>

            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

            <title>QQ授权</title>

            <script type="text/javascript">

                var childWindow;

                function toQzoneLogin()

                {

                    childWindow = window.open("/oauth/qq/index.php","TencentLogin","width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");

                }

                function closeChildWindow()

                {

                    childWindow.close();

                }

            </script>

        </head>

        <body>     

            <a href="#" onclick="toQzoneLogin()"><img src="/img/qq_login.png"></a>

        </body>

    </html>';

    echo $str;

    unset($str);

}

?>


第二步  获取qq接口的全七八糟的东西  在你第一步跳转的页面的文件里面

我是在/oauth/qq/index.php这里面

<?php

    header("Content-Type: text/html;charset=utf-8");

    //应用APP ID

    $app_id = "";

    //应用APP Key

    $app_secret = "";

    //应用填写的网站回调域,就是你创建应用是下面填写的回调地址

    $my_url = "";


    //Step1:获取Authorization Code

    session_start();//这个不用说吧,开启session

    $code = $_REQUEST["code"];//获取Authorization Code

//code不存在时,前往qq接口获取

if(empty($code)) {

        //state参数用于防止CSRF攻击,成功授权后回调时原样带回

        $_SESSION['state'] = md5(uniqid(rand(), TRUE));

        //拼接URL

        $dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&state=".$_SESSION['state'];

        echo("<script> top.location.href='".$dialog_url."'</script>");

    }


    //Step2:通过Authorization Code获取Access Token

    if($_REQUEST['state'] == $_SESSION['state'] || 1) {

        //拼接URL

        $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"."client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&client_secret=".$app_secret."&code=".$code;

        $response = file_get_contents($token_url);

        //如果用户临时改变主意取消登录,返回true!==false,否则执行step3 

        if (strpos($response, "callback") !== false) {

            $lpos = strpos($response, "(");

            $rpos = strrpos($response, ")");

            $response = substr($response, $lpos + 1, $rpos - $lpos -1);

            $msg = json_decode($response);

            if (isset($msg->error)) {

                echo "<h3>error:</h3>".$msg->error;

                echo "<h3>msg :</h3>".$msg->error_description;

                exit;

            }

        }

        //Step3:使用Access Token来获取用户的OpenID

        $params = array();

        parse_str($response, $params);//把传回来的数据参数变量化

        $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];

        $str = file_get_contents($graph_url);

        if (strpos($str, "callback") !== false) {

            $lpos = strpos($str, "(");

            $rpos = strrpos($str, ")");

            $str = substr($str, $lpos + 1, $rpos - $lpos -1);

        }

        $user = json_decode($str);//存放返回的数据 client_id ,openid

        if (isset($user->error)) {

            echo "<h3>error:</h3>".$user->error;

            echo "<h3>msg :</h3>".$user->error_description;

            exit;

        }


        //Step4:使用openid和access_token获取用户信息

        $user_data_url = "https://graph.qq.com/user/get_user_info?access_token={$params['access_token']}&oauth_consumer_key={$app_id}&openid={$user->openid}&format=json";

        $user_data = file_get_contents($user_data_url);//获取到的用户信息

        //以下为授权成功后的自定义操作

        if($user_data){

            $_SESSION['qqInfo'] = $user_data;

            //获取登陆用户信息成功过后需要跳转的页面,填写你第一步的页面地址

            echo("<script> top.location.href=' '</script>");

        }else{

            echo '未知错误';

        }

    }else{

        echo("The state does not match. You may be a victim of CSRF.");

    }


第三步  去你的回调页面接收code

我的回调页面是在/oauth/qq/callback.php

<?php

//接受code

$code = $_REQUEST["code"];

//接受state

$state = $_REQUEST["state"];

//得到code 与 state 跳转

//跳转地址,这里是跳回你的第二步的页面,写全路径

$url = "***/oauth/qq/index.php?code=".$code."&state=".$state;

echo("<script> top.location.href='".$url."'</script>");

?>


如果页面能打印到你的信息了就说明成功了,

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。