_config.php
<?php require_once './vendor/autoload.php'; session_start(); $github_keys = require('./github-app-keys.php'); $provider = new League\OAuth2\Client\Provider\Github([ 'clientId' => $github_keys['clientId'], 'clientSecret' => $github_keys['clientSecret'], ]); $title = "PHP GitHub Login Sample";
github-app-keys.php
<?php return [ 'clientId' => 'xxxx', 'clientSecret' => 'xxxxxxxx' ];
login.php
<?php require_once '_config.php'; $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: '.$authUrl); exit;
callback.php
<?php require_once '_config.php'; if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])){ unset($_SESSION['oauth2state']); exit('Invalid state'); } $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); echo $token.'\n'; echo 'Successfully callbacked!!'.'\n'; $user = $provider->getResourceOwner($token); echo '<pre>'; var_dump($user); echo '</pre>';
[vagrant@localhost api]$ php -S 192.168.33.10:8000