Trying to run laravel based [php connect sample] provided by microsoft github when i run this laravel project on local facing 3 steps the problem is in third step as mention in the screen shot i am also going to prvide screen shot of logincontroller.php
url after connecting microsoft portal is
http://localhost:8000/oauth.php?code=xxxxxxxxxxxxxstate=xxxxxxxxxxxxxxx&session_state=xxxxxxxxxxxxx
ErrorException in LoginController.php line 66: Undefined index: state
LoginController.php file code
namespace App\Http\Controllers;
use Microsoft\Graph\Connect\Constants;
class LoginController extends Controller {
public function oauth()
{
session_start();
//We store user name, id, and tokens in session variables
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => Constants::CLIENT_ID,
'clientSecret' => Constants::CLIENT_SECRET,
'redirectUri' => Constants::REDIRECT_URI,
'urlAuthorize' => Constants::AUTHORITY_URL . Constants::AUTHORIZE_ENDPOINT,
'urlAccessToken' => Constants::AUTHORITY_URL . Constants::TOKEN_ENDPOINT,
'urlResourceOwnerDetails' => '',
'scopes' => Constants::SCOPES
]);
if ($_SERVER['REQUEST_METHOD'] === 'GET' && !isset($_GET['code'])) {
$authorizationUrl = $provider->getAuthorizationUrl();
// The OAuth library automaticaly generates a state value that we can
// validate later. We just save it for now.
$_SESSION['state'] = $provider->getState();
header('Location: ' . $authorizationUrl);
exit();
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['code'])) {
// Validate the OAuth state parameter
if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['state'])) {
unset($_SESSION['state']);
exit('State value does not match the one initially sent');
}
// With the authorization code, we can retrieve access tokens and other data.
try {
// Get an access token using the authorization code grant
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$_SESSION['access_token'] = $accessToken->getToken();
// The id token is a JWT token that contains information about the user
// It's a base64 coded string that has a header, payload and signature
$idToken = $accessToken->getValues()['id_token'];
$decodedAccessTokenPayload = base64_decode(
explode('.', $idToken)[1]
);
$jsonAccessTokenPayload = json_decode($decodedAccessTokenPayload, true);
// The following user properties are needed in the next page
$_SESSION['preferred_username'] = $jsonAccessTokenPayload['preferred_username'];
$_SESSION['given_name'] = $jsonAccessTokenPayload['name'];
header('Location: http://localhost:8000/email');
exit();
} catch (League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
echo 'Something went wrong, couldn\'t get tokens: ' . $e->getMessage();
}
}
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire