Laravel Google Auth using artdarek/ oauth-4-laravel


Laravel Google Auth using artdarek/ oauth-4-laravel

我正在使用Artdarek软件包使用Google帐户登录,并且需要授权用户使用该应用程序。我正在使用拉拉维尔 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 );
    }
}

当新用户授予应用程序的权限时,我收到错误"无法重定向到空 URL"

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

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

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

这意味着,您应该使用带有 2 个参数而不是 1 个参数的 consumer。

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