O Google OAuth 2.0 retorna 400 "Solicitações incorretas", mesmo do Playground
Estou tentando usar oBiblioteca do cliente PHP League OAuth2 para autenticação no meu aplicativo CodeIgniter. No entanto, eu sempre recebo o erro 400 "Solicitação incorreta". Então, tentei usar minha própria identificação de cliente e segredo de cliente através dePlayground OAuth 2.0 do Google e dá o mesmo erro. Curiosamente,Comunidade Jasper Reports recebe o mesmo erro quando tento fazer login com minha Conta do Google. De volta ao playground, tentei o login no Google Plus e o ponto de extremidade antigo. O Google teve uma interrupção?
O URL que falha é
https://accounts.google.com/o/oauth2/auth?
client_id=xxxxx-xxxxx.apps.googleusercontent.com
&redirect_uri=mywebsite.com%2Fauth%2Fsession%2Fgoogle
&state=yyyyyzzzzzwwwww
&scope=profile
&response_type=code
&approval_prompt=auto
(Estado das variáveis, redirect_uri etc. foram ocultados por segurança). Aqui está o código do meu controlador, baseado no exemplo de Phil Sturgeon:
class Auth extends CI_Controller {
public function __construct() {
parent::__construct();
log_message('debug', 'Auth: controller loaded.');
}
public function session($provider_name) {
$this->load->helper('url_helper');
switch (strtolower($provider_name)) {
case "eventbrite":
$provider_name = 'Eventbrite';
break;
case "facebook":
$provider_name = 'Facebook';
break;
case "github":
$provider_name = 'Github';
break;
case "google":
$provider_name = 'Google';
break;
case "instagram":
$provider_name = 'Instagram';
break;
case "linkedin":
$provider_name = 'LinkedIn';
break;
case "microsoft":
$provider_name = 'Microsoft';
break;
case "vkontakte":
$provider_name = 'Vkontakte';
break;
}
log_message('debug', 'Auth: session to ' . $provider_name);
$class = 'League\\OAuth2\\Client\\Provider\\'.$provider_name;
$provider = new $class(array(
'clientId' => $this->config->item('client_id'),
'clientSecret' => $this->config->item('client_secret'),
'redirectUri' => $this->config->item('redirect_uri')
));
log_message('debug', 'Auth: connect ' . $this->config->item('client_id'));
if (! $this->input->get('code')) {
// By sending no options it'll come back here
$url = $provider->getAuthorizationUrl();
log_message('error', 'Auth: redirect to ' . $url);
redirect($url);
} else {
// Have a go at creating an access token from the code
// Try to get an access token (using the authorization code grant)
$token = new stdClass();
// If you are using Eventbrite you will need to add the grant_type parameter (see below)
if ($provider_name == 'eventbrite') {
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code'],
'grant_type' => 'authorization_code'
]);
} else {
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
}
// Use this object to try and get some user details (username, full name, etc)
try {
// We got an access token, let's now get the user's details
$userDetails = $provider->getUserDetails($token);
// Use these details to create a new profile
//printf('Hello %s!', $userDetails->firstName);
} catch (Exception $e) {
// Failed to get user details
show_error("That didn't work: " . $e);
log_message('error', "Auth: That didn't work: " . $e);
}
// Here you should use this information to A) look for a user B) help a new user sign up with existing data.
// If you store it all in a cookie and redirect to a registration page this is crazy-simple.
echo "<pre>Tokens: ";
var_dump($token);
echo "\n\nUser Info: ";
var_dump($userDetails);
}
}
}