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

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

作者: 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.

    相关文章

      网友评论

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

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