问题
I'm trying to use the PHP League OAuth2 client library for authentication in my CodeIgniter application. However, I always get error 400 "Bad Request". So, I tried using my own Client ID and Client Secret through Google's OAuth 2.0 playground and it gives the same error. Interestingly, Jasper Reports Community gets the same error when I try to login with my Google Account. Back to the playground, I've tried the Google Plus login as well as the old endpoint. Has Google had an outage?
The URL that fails is
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
(Variables state, redirect_uri, etc. have been hidden for security). Here is my controller code, based on Phil Sturgeon's example:
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);
}
}
}
来源:https://stackoverflow.com/questions/24807921/google-oauth-2-0-returns-400-bad-request-even-from-playground