如何接入Github登陆

版权声明:如果转载请注明出处,不喜勿喷,谢谢 https://blog.csdn.net/qq_38358436/article/details/89426237

接入github的好处

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

登陆github 并注册一个 oauth

  1. 找到 settings


    7942449-6b7e823737a31901.png
    settings
  2. developer settings


    7942449-55bc54153940f3c7.png
    developer settings
  3. new oauth app


    7942449-acd7ac5933fcfa95.png
    image.png
  4. 获取 client_idclient_secret 填写应用信息

    7942449-e51ebbe30d4d3522.png
    client_id client_secret

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

7942449-310b3d69e858eac8.png
image.png

官方文档

https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps

开发接入

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

代码

/**
 * 发送请求方法
 *
 * @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>");

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

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

最后

如果你真的想学习如何接入的话, 就自己动手敲代码试一遍,别人的始终是别人的
纸上得来终觉浅,绝知此事要恭行

猜你喜欢

转载自blog.csdn.net/qq_38358436/article/details/89426237