qq登录ecshop,或者其它系统最新接口OAuth2.0代码分析与下载

     好多人都在使用QQ登陆ecshop后者其他的web,最近用qq登陆OAuth1.0的用户可能已经知道OAuth1.0已经不能使用,再次我总结一下QQ OAuth2.0登录ecshop。

    经过对QQ登录文档的研究 ,qq文档理论是特别的多 实际操作的并不是很多,对于一些刚研究接口的程序员来说更是难上加难。本人通过对QQ登录文档的研究 发现原来文档也提供了示例,那就更具qq文档提供是示例来开发ecshop的qq登录OAuth2.0吧。

    qq登录需要分为五个步骤:

     在这说的三部主要是获取信息的五个步骤,至于咋样去申请qq的AIP那就不介绍了。

     第一步:获取access_token

        QQ登录OAuth2.0提供了两种后去access_token的方法,分别是:server-side模式和client-side模式。腾讯建议使用的是server-side模式,个人感觉也是server-side模式好用。下面是第一步后去access_token的代码,其中state是为了防止攻击的一个必须填写值,至于填写什么那就看个人爱好了。

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

    第二部:根据access_token获得对应用户身份的openid

    以下是实现第二个步骤的接口调用地址,也需有人到这里就不想望下去看了,这个没有关系 我会把这个接口的代码提供下载地址的。


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

    第三部:根据access_token与openid调用OpenAPI

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

  第四部:根据获取信息等到用户的信息

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

    得到的用户信息是一个json需要转换,至于咋样转json在这里就不做介绍,我在下一个步骤会直接用ecshop的方法来解决。

  第五步:数据的处理与整合

   在这一步 大家就可以各抒己见了,可以整合ecshop也可以整合其他的系统。

require(ROOT_PATH . 'includes/cls_json.php');
    $json   = new JSON;
    $user_info  = $json->decode(get_contents($info));
 
    $_SESSION['oauth_token'] = $params['access_token'];
    $_SESSION['openid'] = $user->openid;
    $username=$user_info->nickname;
    $password=time();//随便弄个密码
    $email=$_SESSION['openid'].'@qq.com';//没有返回邮箱
    $back_act ="user.php";

   我的关于QQ登录ecshop OAuth2.0的接口就已经大概的介绍完了,下面我把代码贴出供大家研究,本来是想上传附件的但是不知道咋样上传 就把代码粘贴在下面了.

qq.php

<?php
session_start();
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/lib_transaction.php');
include_once(ROOT_PATH . 'includes/lib_passport.php');
//应用的APPID
$app_id = "申请的APPID";
//应用的APPKEY
$app_secret = "申请的APPKEY";
//成功授权后的回调地址
$my_url = "回调地址,我这以网址加/qq.php";
/*第一步*/
$code = $_REQUEST["code"];
if(empty($code))
{
   $_SESSION['state'] = md5(uniqid(rand(), TRUE));    
   $dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&state=".$_SESSION['state'];
    header("Location: ".$dialog_url."\n");
}
/*第二部*/
if($_REQUEST['state'] == $_SESSION['state'])
{
    $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);
    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;
        }
    }
    /*第三步*/
    $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);
    if (isset($user->error))
    {
        echo "<h3>error:</h3>" . $user->error;
        echo "<h3>msg  :</h3>" . $user->error_description;
        exit;
    }
    /*第四部 数据处理*/
    $info = "https://graph.qq.com/user/get_user_info?access_token=".$params['access_token']."&oauth_consumer_key=100346418&openid=".$user->openid."&format=json ";
    require(ROOT_PATH . 'includes/cls_json.php');
    $json   = new JSON;
    $user_info  = $json->decode(get_contents($info));
  /*这里特变说明一下*/

   $user_info的值是登陆返回的数据,包括OPENID,昵称,头像等信息。程序员可针对自己的系统处理数据,这里需要注意的是
    $_SESSION['oauth_token'] = $params['access_token'];
    $_SESSION['openid'] = $user->openid;
    $username=$user_info->nickname;
    $password=time();//随便弄个密码
    $email=$_SESSION['openid'].'@qq.com';//没有返回邮箱
    $back_act ="user.php";
    /* 检测用户名 */


    if (check_user($username)!==false){//账号存在直接完成登录

        /*判断openID*/

      if($db->getOne("SELECT COUNT(*) FROM ".$ecs->table('users')." WHERE user_name='$username' AND openid='".$_SESSION['openid']."'")==1){

            set_session($username);
          //$GLOBALS['user']->set_cookie($username);
          header("Location: user.php\n");
          exit;

       }

      else

     {

            show_message("对不起,用户名".$username."已经存在!");

       }


       
    }else{
        $reg_date = time();
        $password =md5($password);
        $GLOBALS['db']->query('INSERT INTO ' . $GLOBALS['ecs']->table("users") . "(`user_name`, `password`, `reg_time`, `last_login`, `openid`) VALUES ('$username', '$password', '$reg_date', '$reg_date', '$_SESSION['openid']')");//账号不存在 就写入数据库 并登陆
        set_session($username);
        //$GLOBALS['user']->set_cookie($username);
        header("Location: user.php\n");
        exit;
    }
    
}
else
{
    echo("The state does not match. You may be a victim of CSRF.");
}
 
function set_session($username="")
{
    $sql = "SELECT user_id, password, email FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_name='$username' LIMIT 1";
    $row = $GLOBALS['db']->getRow($sql);

    if ($row)
    {
        $_SESSION['user_id']   = $row['user_id'];
        $_SESSION['user_name'] = $username;
        $_SESSION['email']     = $row['email'];
    }
}
function check_user($username)
{
    $sql = "SELECT user_id FROM " . $GLOBALS['ecs']->table("users"). " WHERE user_name='$username'";
    $row = $GLOBALS['db']->getRow($sql);
    if (!empty($row)){ return true; }else{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);
}
?>

转载于:https://www.cnblogs.com/liangyeyue/articles/2972335.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值