如何快速接入 GitHub 登陆

接入github的好处

  1. 一键登录,用户体验好(只针对程序员来说)
  2. 申请简单,如果是要申请QQ登陆或者微博登陆(一顿验证猛如虎,身份证拍照,打电话..各种各种)
  3. 使用简单,利于学习

登陆github 并注册一个 oauth app

  1. 找到 settings
    settings

  2. developer settings
    developer settings

  3. new oauth app
    image.png

  4. 获取 client_idclient_secret 填写应用信息
    client_id client_secret

  5. 完善信息
    注意这个 application namecalback URL 选项,后面请求可能用得到

image.png

官方文档

https://developer.github.com/apps/building...

开发接入

  • 接入github第三发登录分为以下几步
    1. 将用户导入 github登陆授权页面
    2. (如果用户同意授权) => github 会返回到创建 OAuth app 时填写的 callback URL 填写的链接并携带一个 code 参数
    3. 使用这个 code 参数加上 你的 client_id client_secret 去获取 access_token
    4. 使用 access_token 去调取 github 提供的接口 获取用户信息

代码

/**
 * 发送请求(也可以直接使用guzzle)
 *
 * @param string $url      请求地址
 * @param array $data      请求数据
 * @param array $headers   请求头
 * @return string|array
 */
function sendRequest($url, $data = [], $headers = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $response = curl_exec($ch) ? curl_multi_getcontent($ch) : '';
    curl_close($ch);
    return $response;
}

// 如果用户同意登陆, github 就会返回到 callback.php 并携带一个code参数
// 此时只需要使用这个 code 去获取 access_token, 然后在使用 access_token 获取用户信息
$url        = "https://github.com/login/oauth/access_token";
$app_id     = "your github oauth app client_id";
$app_secret = "your github oauth app client_secret";

// 组合请求参数
$code   = $_GET['code'];
$params = [
    'client_id'     => $app_id,
    'client_secret' => $app_secret,
    'code'          => $code,
];

// 发送请求并获取响应信息
$response = sendRequest($url, $params, ['Accept: application/json']);
$response = json_decode($response, true);

// 如果有响应信息, 说明请求成功
if (!empty($response['access_token'])) {
    // 请求成功,使用 access_token 获取用户信息
    $access_token = $response['access_token'];
    $url = "https://api.github.com/user";

    // 发送请求,调取github API 获取用户信息
    $userInfo =  sendRequest($url,[],[
        "Accept: application/json",
        "User-Agent: ilearn",  // 此处(ilearn)是填写应用名称 或者 github用户名
        "Authorization:token {$access_token}"
    ]);

    exit($userInfo);
}

// 如果登陆失败就打印错误信息
echo "<p>登陆失败</p></pre>";
var_dump($response);
exit("</pre>");

如果不想自己写,也可以使用这个大神写的扩展包

https://github.com/overtrue/laravel-social...

最后

如果你真的想学习如何接入的话, 就自己动手敲代码试一遍,别人的始终是别人的,

纸上得来终觉浅,绝知此事要恭行

在这里顺便吐槽一下,微博登录,qq登录,对于开发者来说,用户体验极差, 注册一个账号真的是费劲,文档写的我感觉比看英文文档还困难...

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

可以参考我写的一个 composer包:分享:基于 Laravel 的第三方平台登录

使用命令 composer require huoshaotuzi/sociate 安装,现在支持 QQ、微博、微信公众号、百度、Github 登录,以后有时间还会继续扩展更多的登录。

4年前 评论
secret-500 (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!