我正在使用Artdarek软件包使用Google帐户登录,并需要为该应用授权用户。 我正在使用Laravel 4.2
这是我的功能代码
public function loginWithGoogle() {
// get data from input
$code = Input::get( 'code' );
// get google service
$googleService = OAuth::consumer( 'Google' );
// check if code is valid
// if code is provided get user data and sign in
if ( !empty( $code ) ) {
// This was a callback request from google, get the token
$token = $googleService->requestAccessToken( $code );
// Send a request with it
$result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true );
// Check to see if user already exists
if($user = User::where('email', '=', $result['email'])->first())
{
$user = User::find($user['id']);
Auth::login($user);
// If user isn't activated redirect them
if ($user->deactivated == 0)
{
return View::make('dashboard')->with('user', $user);
}
return Redirect::back()->withErrors(['Sorry You have not been approved', 'Speak to your manager']);
}
else
{
// Create new user waiting for approval
$new_user = new User();
$new_user->email = $result['email'];
$new_user->first_name = $result['given_name'];
$new_user->surname = $result['family_name'];
$new_user->googleID = $result['id'];
$new_user->deactivated = 1;
$new_user->save();
return Redirect::back()->withErrors(['Your account have been created. It is awaiting activation by your manager']);
}
}
// if not ask for permission first
else {
// get googleService authorization
$url = $googleService->getAuthorizationUri();
// return to google login url
return Redirect::to( (string)$url );
}
}
当新用户授予该应用的权限时,我收到错误“无法重定向到空网址”
出于某种原因,我的redirectURL
为空。
答案 0 :(得分:2)
看一下您将发现的文档,您必须在OAuth :: consumer方法中的第二个参数处设置重定向URL。
https://github.com/artdarek/oauth-4-laravel#usage
这意味着,您应该使用带有2个参数的消费者而不是1个参数
$googleService = OAuth::consumer("google","https://mydirectlink");