Laravel Socialite-谷歌登录失败";缺少必需的参数:client_id“;尽管已指定


Laravel Socialite - google login failed "Missing required parameter: client_id" although specified

我在使用Laravel Socialite通过Google API登录用户时遇到了这个奇怪的问题。

所有配置看起来都很正常,但我一直收到错误Missing required parameter: client_id。不仅如此,有时我再试一次,尽管提供了所有这些参数,但错误变成了Missing required parameter: redirect_uri

这就是它的设置方式:

service.php

'google' => [
        'client_id' => 'xxxxxxxxxx-x98asxxxx913ofk5tqvgq7lqfpgdi5u2.apps.googleusercontent.com',
        'client_secret' => 'mklOWJFhpbCzTKxxxx-xxxx',
        'redirect' => 'http://myapp.com/auth/google/callback'
    ],

routes.php

Route::get('/auth/{social_channel}', 'Auth'AuthController@redirect_to_provider');
Route::get('/auth/{social_channel}/callback', 'Auth'AuthController@handle_provider_callback');

AuthController.php

/**
     * Redirect the user to the Google authentication page.
     *
     * @return Response
     */
    public function redirect_to_provider($social_channel)
    {
        return Socialite::driver($social_channel)->redirect();
    }
    /**
     * Obtain the user information from GitHub.
     *
     * @return Response
     */
    public function handle_provider_callback($social_channel, SocialAccountService $service)
    {
        $user = $service->create_or_get_user($social_channel, Socialite::driver($social_channel)->user());
        Auth::login($user);
        return redirect()->to('/go');
    }

Facebook登录对我来说很有效,只是谷歌登录问题是一个顽固而奇怪的问题。

该应用程序部署在Forge(Ubuntu,Nginx)上。

有人能发现这个有什么不对吗?

services.php文件中使用env()函数时,必须在.env文件中使用GOOGLE_CLIENT_ID和GOOGLE_COLIENT_SECRET,不能使用空格。

GOOGLE_CLIENT_ID=xxxxxxxxxxxx    
GOOGLE_CLIENT_SECRET=xxxxxxxxxxxxxx

在服务文件中,使用这个

'client_id' => env('GOOGLE_CLIENT_ID'),         
'client_secret' => env('GOOGLE_CLIENT_SECRET'),

我刚刚解决了同样的问题,我的第一个解决方案是使用->with()在控制器上传递client_id和secret。代码如下:

    return Socialite::driver('google')
    ->with(
        ['client_id' => 'xxxxxxxx'],
        ['client_secret' => 'xxxxxxxx'],
        ['redirect' => 'http://localhost:8000/Yourcallback'])
    ->redirect();

或者仅通过删除services.php 上的env()

    'client_id' => env('xxxxxxx'),
    'client_secret' => env('xxxxxxx'),

用这个

'client_id' => 'xxxxxxx',
'client_secret' => 'xxxxxxx',

社会党似乎没有重新认识env()的作用。

对于使用Forge的用户,您可能需要在Forge面板的环境选项卡中添加google .env信息。此.env信息不会通过Github推送到Forge。