我正在尝试在我的CodeIgniter应用程序中使用PHP League OAuth2 client library进行身份验证。但是,我总是得到错误400“错误请求”。所以,我尝试通过Google's OAuth 2.0 playground使用我自己的客户端ID和客户端密钥,它也会出现同样的错误。有趣的是,当我尝试使用我的Google帐户登录时,Jasper Reports Community会出现同样的错误。回到游乐场,我尝试了Google Plus登录以及旧端点。谷歌有停电吗?
失败的网址是
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
(为安全起见,隐藏了变量state,redirect_uri等)。 这是我的控制器代码,基于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);
}
}
}