您尚未登录。

楼主 #1 2019-09-26 10:01:01

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录

2019-09-26_095832.png

这里申请第三方登陆id和secret: https://github.com/settings/applications/new

源码地址: http://cwestblog.com/2017/07/07/php-simple-github-login-oauth/

<?php
define('OAUTH2_CLIENT_ID', '你的马赛克CLIENT_ID');
define('OAUTH2_CLIENT_SECRET', '你的马赛克CLIENT_SECRET');

$authorizeURL = 'https://github.com/login/oauth/authorize';
$tokenURL = 'https://github.com/login/oauth/access_token';
$apiURLBase = 'https://api.github.com/';

session_start();

// Start the login process by sending the user to Github's authorization page
if(get('action') == 'login') {
  // Generate a random hash and store in the session for security
  $_SESSION['state'] = hash('sha256', microtime(TRUE) . rand() . $_SERVER['REMOTE_ADDR']);
  unset($_SESSION['access_token']);

  // Redirect the user to Github's authorization page
  redirect_to($authorizeURL . '?' . http_build_query([
    'client_id' => OAUTH2_CLIENT_ID,
    'redirect_uri' => get_current_base_url(),
    'state' => $_SESSION['state'],
    'scope' => 'user:email'
  ]));
}

// When Github redirects the user back here, there will be a "code" and "state" parameter in the query string
if(get('code')) {
  // Verify the state matches our stored state
  if(!get('state') || $_SESSION['state'] != get('state')) {
    redirect_to($_SERVER['PHP_SELF']);
  }

  // Exchange the auth code for a token
  $token = apiRequest($tokenURL . '?' . http_build_query([
    'client_id' => OAUTH2_CLIENT_ID,
    'client_secret' => OAUTH2_CLIENT_SECRET,
    'state' => session('state'),
    'code' => get('code')
  ]));

  $_SESSION['access_token'] = $token->access_token;

  redirect_to(get_current_base_url());
}

if(session('access_token')) {
  $user = apiRequest($apiURLBase . 'user?access_token=' . session('access_token'));

  echo '<h3>Logged In</h3>';
  echo '<h4>' . $user->name . '</h4>';
  echo '<pre>';
  print_r($user);
  echo '</pre>';

} else {
  echo '<h3>Not logged in</h3>';
  echo '<p><a href="?action=login">Log In</a></p>';
}

function apiRequest($url) {
  $context  = stream_context_create([
    'http' => [
      'user_agent' => 'CWestify GitHub OAuth Login',
      'header' => 'Accept: application/json'
    ]
  ]);
  $response = @file_get_contents($url, false, $context);
  return $response ? json_decode($response) : $response;
}

function get($key, $default=NULL) {
  return isset($_GET[$key]) ? $_GET[$key] : $default;
}

function session($key, $default=NULL) {
  return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
}

function get_current_base_url() {
  return get_site_url() . preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
}

function get_site_url() {
  return 'http' . ($_SERVER["HTTPS"] ? 's' : '')
    . "://{$_SERVER['SERVER_NAME']}"
    . ($_SERVER["SERVER_PORT"] !== '80' ? ":{$_SERVER['SERVER_PORT']}" : '');
}

function redirect_to($url) {
  header('Location: ' . $url);
  die();
}

本站演示地址: https://whycan.cn/oauth/github.php





离线

楼主 #2 2019-09-26 10:01:17

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录





离线

楼主 #4 2019-09-26 10:07:54

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录

对, 这个对象是本站可以访问的数据, 拿到这些数据就可以和本站的账号绑定。

和微信/QQ 扫二维码登录原理一样。





离线

楼主 #6 2019-09-26 10:31:22

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录

aozima 说:

还是搞个备案吧,这样弄啥都方便。

个人不能备案交互式网站, 要公司才行,没备案又不能申请微信/QQ/微博登录,一环套一环。

不过正在准备着手做这件事情了。





离线

楼主 #8 2019-09-26 10:49:11

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录

是的, 反正就是程序非常麻烦,即使当时备案通过,后面查到也是一堆麻烦事。





离线

楼主 #9 2019-09-26 11:32:11

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录

<?php
define('OAUTH2_CLIENT_ID', '你的马赛克CLIENT_ID');
define('OAUTH2_CLIENT_SECRET', '你的马赛克CLIENT_SECRET');

$authorizeURL = 'https://github.com/login/oauth/authorize';
$tokenURL = 'https://github.com/login/oauth/access_token';
$apiURLBase = 'https://api.github.com/';

session_start();

if(get('action') == 'logout') {
        printf("<p />clear ok");

        // Start session
        if(!session_id()){
            session_start();
        }

        // Remove access token and state from session
        unset($_SESSION['access_token']);
        unset($_SESSION['state']);

        // Remove user data from session
        unset($_SESSION['userData']);

}


// Start the login process by sending the user to Github's authorization page
if(get('action') == 'login') {
  // Generate a random hash and store in the session for security
  $_SESSION['state'] = hash('sha256', microtime(TRUE) . rand() . $_SERVER['REMOTE_ADDR']);
  unset($_SESSION['access_token']);

  // Redirect the user to Github's authorization page
  redirect_to($authorizeURL . '?' . http_build_query([
    'client_id' => OAUTH2_CLIENT_ID,
    'redirect_uri' => get_current_base_url(),
    'state' => $_SESSION['state'],
    'scope' => 'user:email'
  ]));
}

// When Github redirects the user back here, there will be a "code" and "state" parameter in the query string
if(get('code')) {
  // Verify the state matches our stored state
  if(!get('state') || $_SESSION['state'] != get('state')) {
    redirect_to($_SERVER['PHP_SELF']);
  }

  // Exchange the auth code for a token
  $token = apiRequest($tokenURL . '?' . http_build_query([
    'client_id' => OAUTH2_CLIENT_ID,
    'client_secret' => OAUTH2_CLIENT_SECRET,
    'state' => session('state'),
    'code' => get('code')
  ]));

  $_SESSION['access_token'] = $token->access_token;

  redirect_to(get_current_base_url());
}

if(session('access_token')) {
  $user = apiRequest($apiURLBase . 'user?access_token=' . session('access_token'));

  echo '<h3>Logged In</h3>';
  echo '<h4>' . $user->name . '</h4>';
  echo '<pre>';
  print_r($user);
  echo '</pre>';
  
  echo '<p><a href="?action=logout">Log Out</a></p>';  
} else {
  echo '<h3>Not logged in</h3>';
  echo '<p><a href="?action=login">Log In</a></p>';
}

function apiRequest($url) {
  $context  = stream_context_create([
    'http' => [
      'user_agent' => 'CWestify GitHub OAuth Login',
      'header' => 'Accept: application/json'
    ]
  ]);
  $response = @file_get_contents($url, false, $context);
  return $response ? json_decode($response) : $response;
}

function get($key, $default=NULL) {
  return isset($_GET[$key]) ? $_GET[$key] : $default;
}

function session($key, $default=NULL) {
  return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
}

function get_current_base_url() {
  return get_site_url() . preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
}

function get_site_url() {
  return 'http' . ($_SERVER["HTTPS"] ? 's' : '')
    . "://{$_SERVER['SERVER_NAME']}"
    . ($_SERVER["SERVER_PORT"] !== '80' ? ":{$_SERVER['SERVER_PORT']}" : '');
}

function redirect_to($url) {
  header('Location: ' . $url);
  die();
}

参考: https://www.codexworld.com/login-with-github-oauth-api-using-php/

把 login/logout 都加入了。





离线

楼主 #10 2019-09-26 11:55:44

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录





离线

楼主 #12 2019-09-26 12:14:34

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录

aozima 说:

都是Oauth2协议,写好一个,再加后面就很快了。

嗯,有道理!





离线

楼主 #14 2019-09-26 12:53:47

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 试一试用第三方登录系统, QQ/微信/微博等这些国内的社交软件都要备案才行,所以第一个测试github账号登录

域名是阿里云买的,主机是"外面"买的

域名只要登记身份证号码就可以,主机才要备案





离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn