Skip to content

Instantly share code, notes, and snippets.

@gongpeione
Last active May 26, 2017 08:51
Show Gist options
  • Save gongpeione/2d0fc67a6cc789ab1241935192118bdf to your computer and use it in GitHub Desktop.
Save gongpeione/2d0fc67a6cc789ab1241935192118bdf to your computer and use it in GitHub Desktop.
Github OAuth PHP
<?php
// error_reporting(E_ALL);
$code = $_GET['code'];
$token = $_COOKIE['access_token'];
if (!empty($code)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://github.com/login/oauth/access_token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"client_id=xxx&client_secret=xxx&code=$code");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = json_decode(curl_exec($ch));
curl_close ($ch);
// print_r($server_output);
// $access_token = explode("&", $server_output);
// $access_token = explode('=', $access_token[0])[1];
setcookie(
"access_token",
$server_output->access_token,
$maxage = time() + 3600000,
$path = '',
$domain = 'geeku.net',
$secure = false,
$httpOnly = true
);
header('Location: https://xxx');
} else if (!empty($token)) {
$github_user = 'https://api.github.com/user';
$github_graphQL = 'https://api.github.com/graphql';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $github_graphQL);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: " . "token " . $token,
"User-Agent: GeeLogin"
]);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"query":"{\n viewer {\n login,email\n }\n}\n","variables":"{}","operationName":null}');
$server_output = curl_exec ($ch);
curl_close ($ch);
$server_output_json = json_decode($server_output);
if ($server_output_json->message == 'Bad credentials') {
setcookie(
"access_token",
'',
$maxage = 0,
$path = '',
$domain = 'geeku.net',
$secure = false,
$httpOnly = true
);
header('Location: https://xxx');
} else {
echo $server_output;
}
} else {
?>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta http-equiv="Cache-Control" content="no-siteapp">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<style>
body, html {
padding: 0;
margin: 0;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
a.login {
display: inline-block;
padding: 10px 20px;
border: 1px solid #ddd;
color: #333;
text-decoration: none;
border-radius: 10px;
font-size: 20px;
font-family: 'Pingfang SC', 'Microsoft YaHei'
}
a.login:hover {
border-color: #2ade6a;
}
</style>
</head>
<body>
<a href="https://github.com/login/oauth/authorize?client_id=xxx" class="login">Login with Github</a>
</body>
</html>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment