Laravel Google 使用 artdarek/oauth-4-laravel 进行身份验证

Laravel Google Auth using artdarek/ oauth-4-laravel

我正在使用 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 );
    }
}

当新用户授予该应用程序权限时,我收到错误 'Cannot redirect to an empty URL'

出于某种原因,我的 redirectURL 是空的。

查看文档,您会发现必须在 OAuth::consumer 方法的第二个参数处设置重定向 url。

https://github.com/artdarek/oauth-4-laravel#usage

这意味着,你应该使用带有 2 个参数而不是 1 个参数的消费者

$googleService = OAuth::consumer("google","https://mydirectlink");