美文网首页cakephp
腾讯qq快捷登录

腾讯qq快捷登录

作者: 永不言弃__ | 来源:发表于2019-11-27 10:04 被阅读0次

腾讯接口文档地址:http://wiki.connect.qq.com/

第一步生成一个state上传给腾讯接口并保存到本地用于防止恶意攻击

如下(可自选生成方式):

$value = time().rand(100000,999999);

Cache::write('state'.$value,'xxx','r5m');

第二步带着参数请求腾讯qq快捷登录接口:

如下:参数包括申请的应用appid和设置的redirect_uri以及第一步生成的state

$this->redirect('https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id='.$app_id.'&redirect_uri='.urlencode($url_qq).'&state='.$value)

其中要对redirect_uri进行urlencode()

第三步:

在设置的第二步的redirect_uri中和腾讯进行信息交流获取数据

代码如下(本例为cakephp框架):

    $code = $this->request->getQuery('code');

    $state = $this->request->getQuery('state');

    if(empty($code)){

      $this->G->error("login_canceled");

      return null;

    }

    $flag = 'state'.$state;

    $flag_value = Cache::read($flag,'r5m');

    if($flag_value != 'xxx'){

      $this->G->error($state."login_state_error".$flag_value);

      return null;

    }

    $http = new Client();

    $response = $http->get('https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id='.$appid.'&client_secret='.$secret.'&code='.$code.'&redirect_uri='.$urlencode($url_qq));

    if(strpos($response->body,'error') !==false){

      $this->G->error("login_error");

      return null;

    }

    $access_token = explode('&',$response->body)[0];

    $response1 = $http->get('https://graph.qq.com/oauth2.0/me?'.$access_token);

    if(strpos($response1->body,'error') !==false){

      $this->G->error("login_error");

      return null;

    }

    $arr = str_replace('callback( ','[',$response1->body);

    $arr = str_replace(' );',']',$arr);

    $arr = json_decode($arr);

    $response2 = $http->get('https://graph.qq.com/user/get_user_info?'.$access_token.'&oauth_consumer_key='.$appid.'&openid='.$arr[0]->openid);

      $response2->body = json_decode('['.$response2->body.']') ;

      if($response2->body[0]->ret !== 0){

        $this->G->error("login_userinfo_error");

        return null;

      }

此处$response2->body的值如下图:

    return $this->redirect(成功后跳转的地址【如网站首页】);

相关文章

  • 腾讯qq快捷登录

    腾讯接口文档地址:http://wiki.connect.qq.com/ 第一步生成一个state上传给腾讯接口并...

  • QQ快速安全登录的设计与实现

    在使用QQ账号登录腾讯网站时,QQ推荐使用快速安全登录功能来方便快捷安全的登录腾讯网站。快速安全登录除了手机扫描二...

  • QQ平台配置

    集成乐推的QQ登录功能需要在腾讯开放平台申请appid和appkey,如果需要QQ登录功能需要腾讯开放平台审核通过...

  • QQ webView 授权

    如果iOS QQ登录需要使用网页登录,则需要向腾讯官方进行申请:QQ:800013811 QQ互联redirect...

  • 2019-07-24第三方登录

    微博登录 微博登陆声明 qq登录 QQ登陆声明 这个是让网站加入QQ登录接口,这段代码可放在 之间。申请腾讯接口...

  • Android实现QQ第三方登录

    前言 之前写过一篇Android实现的QQ登录文章,但是由于腾讯官网已经将QQ登录功能迁移到了 QQ互联上了,所以...

  • 程序员的Mac入门-多开 QQ

    在打开 QQ 或是选中 QQ 的状态下,按快捷键:command + N,会弹出新的 QQ 登录窗口

  • 2018-04-23 友盟登录经历报错

    一 . QQ登录功能在测试中!(错误码:110406) 报错分析: 可能有几点原因: 1. 腾讯平台QQ 登录功能...

  • 原创【干货】礼物说,你用过么?(APP全解析)

    页面一:登录/注册 用户可以直接通过手机来进行登录,快捷登录上使用新浪微博、微信、QQ为第三方登录方,用户在非登录...

  • bug上报-腾讯bugly

    前言 app bug跟踪,简单快速集成腾讯bugly 集成前准备 1.登录腾讯bugly官网2.立即接入→QQ登...

网友评论

    本文标题:腾讯qq快捷登录

    本文链接:https://www.haomeiwen.com/subject/ztouwctx.html