10

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

一切配置看起来都很正常和普通,但我不断收到错误消息Missing required parameter: client_id。不仅如此,有时我再试一次,Missing required parameter: redirect_uri尽管提供了所有这些参数,但错误仍然存​​在。

这是它的设置方式:

服务.php

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

路由.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 登录对我有用,只是这个 Google 登录问题是一个顽固而奇怪的问题。

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

有人能发现这有什么问题吗?

4

3 回答 3

7

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

GOOGLE_CLIENT_ID=xxxxxxxxxxxx    
GOOGLE_CLIENT_SECRET=xxxxxxxxxxxxxx

在服务文件中,使用这个

'client_id' => env('GOOGLE_CLIENT_ID'),         
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
于 2018-04-03T19:02:32.417 回答
5

我刚刚解决了同样的问题,我的第一个解决方案是使用->with(). 代码如下:

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

或者只需删除env()on services.php

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

有了这个

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

社交名流似乎没有认识到env()功能。

于 2018-11-19T09:27:01.080 回答
0

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

于 2021-06-03T12:05:39.723 回答