I've tried both of these libraries for the Oauth 2.0 process:
1) https://github.com/thephpleague/oauth2-client 2) https://github.com/tpavlek/oauth2-bnet
But I haven't really made either of them work. I'm fairly new to Laravel but I want to use this framework for this project.
Here's the code I've been working on:
Routes
Route::get('oAuth/authRequest', 'CommunityOAuthProfile@authRequest');
Route::get('oAuth/authCode', 'CommunityOAuthProfile@authCode');
Controllers(CommunityOAuthProfile.php)
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use OAuth;
class CommunityOAuthProfile extends Controller
{
public function authRequest()
{
$provider = new \Depotwarehouse\OAuth2\Client\Provider\WowProvider([
'clientId' => <client_id>, // The client ID assigned to you by the provider
'clientSecret' => <client_secret>, // The client password assigned to you by the provider
'redirectUri' => 'https://sc2data.com/oAuth/authCode',
]);
if (!isset($_GET['code']))
{
$authorizationUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authorizationUrl);
exit;
}
elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else
{
try
{
// Try to get an access token using the authorization code grant.
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$user = $this->provider->getResourceOwner($token);
// We have an access token, which we may use in authenticated
// requests against the service provider's API.
// echo $accessToken->getToken() . "
";
// echo $accessToken->getRefreshToken() . "
";
// echo $accessToken->getExpires() . "
";
// echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "
";
// // Using the access token, we may look up details about the
// // resource owner.
// $resourceOwner = $provider->getResourceOwner($accessToken);
// var_export($resourceOwner->toArray());
// // The provider provides a way to get an authenticated API request for
// // the service, using the access token; it returns an object conforming
// // to Psr\Http\Message\RequestInterface.
// $request = $provider->getAuthenticatedRequest(
// 'GET',
// 'http://brentertainment.com/oauth2/lockdin/resource',
// $accessToken
// );
}
catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e)
{
exit($e->getMessage());
}
}
}
public function authCode()
{
$provider = new \Depotwarehouse\OAuth2\Client\Provider\WowProvider([
'clientId' => <client_id>, // The client ID assigned to you by the provider
'clientSecret' => <client_secret>, // The client password assigned to you by the provider
'redirectUri' => 'https://sc2data.com/oAuth/authCode',
]);
try
{
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$user = $this->provider->getResourceOwner($token);
}
catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e)
{
exit($e->getMessage());
}
}
}
By the way, is there any recommended oauth2 library for php? So far, these 2 are the most promising I've seen but there may be others I'm not aware of?