我正在尝试为 Laravel 4 设置 oauth2 身份验证系统(专门针对 Facebook):https ://github.com/madewithlove/laravel-oauth2
我有一个控制器 Oauth2Controller.php 来控制它。我正在尝试将包中的必要文件包含到我的控制器中。按照该存储库的示例,我使用:
use OAuth2\OAuth2;
use OAuth2\Token_Access;
use OAuth2\Exception as OAuth2_Exception;
但是,当我运行该页面时,它会为这些行抛出错误。说:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
syntax error, unexpected 'as' (T_AS), expecting ',' or ';' or '{'
它直接引用了第三行:use OAuth2\Exception as OAuth2_Exception;
我不确定它为什么会抛出这个错误。
这是我的控制器的完整代码:
<?php
class Oauth2Controller extends BaseController
{
use OAuth2\OAuth2;
use OAuth2\Token_Access;
use OAuth2\Exception as OAuth2_Exception;
public function getIndex($provider) {
$provider = OAuth2::provider($provider, array(
'id' => '**************',
'secret' => '******************',
));
if(! isset($_GET['code'])) {
return $provider->authorize();
}
else
{
// Howzit?
try
{
$params = $provider->access($_GET['code']);
$token = new Token_Access(array(
'access_token' => $params->access_token
));
$user = $provider->get_user_info($token);
// 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>";
var_dump($user);
}
catch (OAuth2_Exception $e)
{
show_error('That didnt work: '.$e);
}
}
}
}
在我的路线中,要访问该页面,我有:
Route::get('oauth/{provider}', 'Oauth2Controller@getIndex');
感谢您的帮助和见解!