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

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.

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,261评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 如果这个纪录迟早要告吹,我只想输给他。——纳达尔 不是每个人都有幸见证历史。这或许是两位传奇名将最后一次在大满贯赛...
    傅踢踢阅读 6,558评论 2 16
  • 总想写些什么,可是却不知所云…… 今天就随意写些吧! 人生,有些时候,真的不知道
    东边的耳朵123阅读 1,833评论 0 0
  • 参加了小组织,每周写计划,周末进行总结,任务还在渐渐的完善。每一周的计划时而多,时而少。但是有一点发现。自己做的任...
    蘑菇乐阅读 1,040评论 0 0