美文网首页第三方平台开发
微信授权登录及获取个人信息

微信授权登录及获取个人信息

作者: b22523051261 | 来源:发表于2017-07-29 19:54 被阅读232次

1.申请测试账号,并配置一系列东西,这个很简单略
2.重要!!!
配置授权网页,这关系到跳转.后面再提


image.png

3.先上链接

https://open.weixin.qq.com/connect/oauth2/authorize
?appid= 微信的appid
&redirect_uri=跳转的URL 要在前面授权网页域名下的地址
&response_type=code
&scope=snsapi_base/snsapi_userinfo 后者需要提供用户信息(授权)
&state=123#wechat_redirect

这里我专门把长链接切开,看的清楚
4.在代码中读取,这里我用PHP做为demo//我也不知道 毕竟不是我写的...

 public function index()
    {
        // 1.用户同意授权后,获得code
        $code = $_GET['code'];
        // 用户授权时获取到到状态
        $state = $_GET['state'];

        $appid = C('TEACHER_APPID');
        $appsecret = C('TEACHER_APPSECRET');

        if (empty($code)) {
            //$this->error('授权失败');
            echo '<h2 style="text-align:center;">授权失败</h2>';
            exit;
        }

        // 2.通过code来获取网页授权的access_token
        $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code ';
        $access_token = json_decode(file_get_contents($access_token_url));
        if (isset($access_token->errcode)) {
            //echo '<h1>错误:</h1>' . $access_token->errcode;
            //echo '<br/><h2>错误信息:</h2>' . $access_token->errmsg;
            //exit;
            //$this->error('请重新授权登录');
            echo '<h2 style="text-align:center;">请重新授权登录</h2>';
            exit;
        }

        // 3.由于access_token拥有较短的有效期,刷新可以重置有效期,
        $refresh_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $appid . '&grant_type=refresh_token&refresh_token=' . $access_token->refresh_token;
        $refresh_token = json_decode(file_get_contents($refresh_token_url));
        if (isset($refresh_token->errcode)) {
            //echo '<h1>错误:</h1>' . $refresh_token->errcode;
            //echo '<br/><h2>错误信息:</h2>' . $refresh_token->errmsg;
            //exit;
            //$this->error('请重新授权登录');
            echo '<h2 style="text-align:center;">请重新授权登录</h2>';
            exit;
        }

        // 4.获取用户信息
        $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $refresh_token->access_token . '&openid=' . $refresh_token->openid . '&lang=zh_CN';
        $user_info = json_decode(file_get_contents($user_info_url));
        if (isset($user_info->errcode)) {
            //echo '<h1>错误:</h1>' . $user_info->errcode;
            //echo '<br/><h2>错误信息:</h2>' . $user_info->errmsg;
            //exit;
            //$this->error('请重新授权登录');
            echo '<h2 style="text-align:center;">请重新授权登录</h2>';
            exit;
        }

        if (empty($user_info)) {
            echo '<h2 style="text-align:center;">获取用户信息失败,请重新登录</h2>';
            exit;
        }

        // 获取微信用户信息
        $data = array(
            'openid' => $user_info->openid,
            'nickname' => $user_info->nickname,
            'sex' => $user_info->sex,
            'city' => $user_info->city,
            'province' => $user_info->province,
            'country' => $user_info->country,
            'headimgurl' => $user_info->headimgurl,
            'type' => 0,
            'modified' => time(),
        );
        echo $refresh_token;
        dump($data);
        dump($user_info);
    }

Thanks.

相关文章

  • 微信授权登录及获取个人信息

    1.申请测试账号,并配置一系列东西,这个很简单略2.重要!!!配置授权网页,这关系到跳转.后面再提 3.先上链接 ...

  • 微信小程序未授权处理方法

    登录页面效果图: 登录按钮: 点击登录按钮,先进行微信登录授权,从而获取用户的头像及昵称等相关信息。这里不过多说,...

  • 微信公众号与消息回复

    接上一篇微信授权登录以及个人信息获取 首先是微信的原理/流程每当关注,发送信息的时候都会调用接口,并发送相对应事件...

  • FEM模式解析

    FEM未来地球矿池模式 注册授权微信送600矿池加5台微矿,无需实名认证,拒绝收集用户个人信息,授权微信登录防止刷...

  • 程序中获取微信个人信息

    逻辑梳理:1.判断openID 和 accessToken 是否存在微信是否登录,已登录,从后台获取个人信息并显示...

  • 微信登录

    1.登录 及获取 用户基础信息 前端调起微信登录获取code ----传给后端 后端通过code 调用微信api...

  • uniapp中微信授权登录

    该开始微信授权登录可以,后来又不行了,点击微信授权登录没有反应,在设置微信登录的按钮中添加 微信授权登录 监听 g...

  • 微博第三方登录--个人笔记

    微博第三方登录## 微博第三方登录,在这里记录一下笔记,获取到授权,切获取到头像名字等个人信息首先在微博开发者平台...

  • 常用的方法

    获取当前时间 获取 url 的方法 微信包 微信授权登录支付等-三方[https://github.com/Wec...

  • 第三方登录:微信登录接入指南

    网站应用微信登录是基于OAuth2.0协议标准构建的微信OAuth2.0授权登录系统。 申请应用及配置 在微信开发...

网友评论

    本文标题:微信授权登录及获取个人信息

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