gitee三方登录
步骤
1、先在gitee设置的第三方应用上创建应用,编写回调地址,和网址主页,生成Client ID 和 Client Secret
2、在点击用户授权之后,会在回调地址栏上出现一个code获取到code,请求 https://gitee.com/oauth/token 地址,post请求, 'https://gitee.com/oauth/token?grant_type=authorization_code&' . $code . '&client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret; 拼接url请求可以获得一个token。
3、通过token请求https://gitee.com/api/v5/user地址,get请求,https://gitee.com/api/v5/user?access_token=' . $token,获得用户的基本信息$userinfo.
4、创建一个三方关联表,将用户信息和三方关联的信息存在表中,用来判断该三方账号是否已经绑定用户账号,没有则新增一个用户,返回用户信息,有的话就返回用户信息。
CREATE TABLE `trilateral_login` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `type` int DEFAULT NULL COMMENT '三方服务 1:gitee', `user_id` int DEFAULT NULL COMMENT '用户的id', `auth_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '验证的三方登录的唯一标识', `init_time` int DEFAULT NULL, `last_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin COMMENT='三方关联表';
配置文件config.php新增的配置
$config['gitee_get_token'] = 'https://gitee.com/oauth/token'; $config['gitee_get_info'] = 'https://gitee.com/api/v5/user'; $config['gitee_client_id'] = '70fe5fdf4ed079a2998a*********1e0af6a61bb80ae92b691e025eef2607***'; $config['gitee_client_secret'] = '7c02b14f9ea47f67*********fbd3f2b0a914b89f1f42ab76febdc12cf******'; $config['gitee_redirect_uri'] = 'http://ci.com/gitCallback';
controller代码
<?php
model代码
来源:https://www.cnblogs.com/wyqgg/p/15592098.html |