Php 拉拉维尔社交名流:无效状态例外

Php 拉拉维尔社交名流:无效状态例外,php,facebook,laravel,laravel-5,Php,Facebook,Laravel,Laravel 5,我正在使用Laravel Socialist在网站上添加Facebook连接按钮。有时,我在回调时会出现以下错误: exception 'Laravel\Socialite\Two\InvalidStateException' in /example/vendor/laravel/socialite/src/Two/AbstractProvider.php:161 我不知道这是什么意思,也没有发现关于这个错误的任何东西。真正的问题是它似乎是一个随机的异常(不明白为什么会发生)。那么这个错误意

我正在使用Laravel Socialist在网站上添加Facebook连接按钮。有时,我在回调时会出现以下错误:

exception 'Laravel\Socialite\Two\InvalidStateException' 
in /example/vendor/laravel/socialite/src/Two/AbstractProvider.php:161
我不知道这是什么意思,也没有发现关于这个错误的任何东西。真正的问题是它似乎是一个随机的异常(不明白为什么会发生)。那么这个错误意味着什么以及如何避免它呢


这似乎不是同一个问题,因为在我的情况下,它是随机的。

如果您仍然需要帮助,您可以使用我的代码,它对我有效。您只需要创建两个路由并更新users表。别忘了让密码为空,因为你不会从facebook用户那里得到密码 我的控制器中的代码:

public function redirectToProvider()
{
    return Socialize::with('facebook')->redirect();
}

public function handleProviderCallback(User $user)
{
    $money = Socialize::with('facebook')->user();

    if(User::where('email', '=', $money->email)->first()){
    $checkUser = User::where('email', '=', $money->email)->first();
    Auth::login($checkUser);
    return redirect('home');
     } 

    $user->facebook_id = $money->getId();
    $user->name = $money->getName();
    $user->email = $money->getEmail();
    $user->avatar = $money->getAvatar();
    $user->save();

    Auth::login($user);
    return redirect('home');

}

我昨晚遇到了这个问题,并用以下解决方案解决了它

关于我的问题,我有更多的信息

AbstractProvider.php第182行中的InvalidStateException

在函数
handleProviderCallback()
中,当它从Facebook登录重新直接返回时。这似乎和你的问题一样

此外,当我在没有
www
的情况下打开我的网站时,我发现我的问题出现了。当我用
www.mysite.com
打开我的网站时-没问题。起初我认为我的问题是随机的,直到我从Chris Townsend对这个问题的回答中得到了线索——非常感谢

解决方案

  • 转到您的www根目录,检查laravel文件
    config/session.php
  • 检查会话Cookie域 默认配置为
    'domain'=>null,
    我更改了
    'domain'=>'mysite.com'
  • 'php artisan cache:clear'
    'composer dump autoload'
    之后,我可以从
    www.mysite.com
    mysite.com
    两个站点无问题登录

  • 在这些修改完成后进行测试时,请确保从浏览器中删除cookie。旧的cookie仍然会产生问题。

    我修复了这个问题,只是将会话驱动程序禁用为数据库。。。文件驱动程序在几个小时试图修复此问题后运行良好。

    还要检查
    存储/framework/sessions
    文件夹上的访问权限

    在我的例子中,由于这个文件夹在新的Laravel项目中是空的,所以在最初提交到GIT存储库的过程中它被忽略了。后来,我在生产服务器上手动创建了它,但显然使用了错误的访问权限,因此它不能为会话驱动程序写入(当设置为
    'file'
    )。

    tl;dr

    如果需要读取第三方服务返回的给定参数
    state
    ,则可以设置Socialite以避免使用
    stateless
    方法进行此检查:

       Socialite::driver($provider)->stateless();
    

    我认为社交名流已经准备好避免这个问题

    /**
    *确定当前请求/会话是否具有不匹配的“状态”。
    *
    *@returnbool
    */
    受保护函数hasvalidState()
    {
    如果($this->isStateless()){
    返回false;//请求->获取会话()->pull('state');
    返回!(strlen($state)>0&&$this->request->input('state')=$state);
    }
    

    例如,
    state
    对于通过google传递数据非常有用:

    参数:状态(任意字符串)
    提供可能对您的应用程序有用的任何状态 收到响应后的应用程序。谷歌授权 服务器往返于此参数,因此应用程序将接收 发送的值相同。可能的用途包括将用户重定向到 更正站点中的资源,并跨站点请求伪造 缓解措施

    参考:

    已解决:

    Socialite::driver('google')->stateless()->user()
    

    我想和大家分享我的解决方案。我转到我的
    AbstractProvider.php
    文件,并在问题的那一行

    public function user()
    {
        if ($this->hasInvalidState()) {
            throw new InvalidStateException;
        }
    
        // ...
    }
    
    我停止抛出new
    InvalidStateException
    并像这样调用重定向函数:

    public function user()
    {
        if ($this->hasInvalidState()) {
            $this->redirect();
            // throw new InvalidStateException;
        }
    
        // ...
    }
    

    我也遇到了同样的问题,我只是清除了缓存来解决这个问题。我只是运行了这个命令并再次启动了这个过程

    php artisan cache:clear
    

    我希望这个答案可能会对其他人有所帮助。

    我只是在使用facebook应用程序而不是浏览器中的facebook通过移动网络登录时遇到此错误。facebook应用程序在登录后使用facebook浏览器,而不是您当前的浏览器,因此不知道以前的状态

    try {
        $socialite = Socialite::driver($provider)->user();
    } catch (InvalidStateException $e) {
        $socialite = Socialite::driver($provider)->stateless()->user();
    }
    
    这为我解决了问题
    $request->session()->put('state',Str::random(40));
    
    $user=Socialite::driver('github')->无状态()->user();

    我也遇到过类似的问题

    InvalidStateException in AbstractProvider.php line 182
    
    在函数
    handleProviderCallback()
    中,当它从Facebook登录重新定向回时。它似乎与您的问题相同

    此外,我发现我的问题发生在我在没有
    www
    的情况下打开我的网站时。当我在
    www.mysite.com
    的情况下打开我的网站时-没问题。起初我认为我的问题是随机的,直到我从Chris Townsend对问题的回答中得到了线索

    解决方案

    转到您的www根目录,检查laravel文件
    config/session.php
    。检查会话
    会话Cookie域
    。默认配置为

    'domain' => null,
    
    我改变了主意

    'domain' => 'mysite.com' 
    
    php artisan cache:clear
    composer dump autoload
    之后,我可以从www.mysite.com和mysite.com无问题登录


    在这些修改完成后进行测试时,请确保从浏览器中删除Cookie。旧Cookie仍然会产生问题。

    获得解决方案-2018年11月更新

    public function redirectToGoogle(Request $request)
    {
        /**
         * @var GoogleProvider $googleDriver
         */
        $googleDriver = Socialite::driver("google");
        return $googleDriver->redirect();
    }
    
    public function fromGoogle(Request $request)
    {
        try {
    /*
    Solution Starts ----
    */
            if (empty($_GET)) {
                $t = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
                parse_str($t, $output);
                foreach ($output as $key => $value) {
                    $request->query->set($key, $value);
                }
            }
    /*
    Solution Ends ----
    */
            /**
             * @var GoogleProvider $googleDriver
             */
            $googleDriver = Socialite::driver("google");
            print_r($googleDriver->user());
        } catch (\Exception $e) {
            print_r($e->getMessage());
        }
    

    在我的例子中,这是由于
    用户
    类中的
    $filleble
    数组中缺少参数造成的。当我添加所有缺少的参数时,它开始正常工作。

    有两个主要的“陷阱”,现有答案都没有解决。
  • 有时,
    InvalidStateException
    是一种误导,真正的根本原因是其他一些错误。有一次我花了12个小时才意识到我没有在模型中的
    $filleble
    数组中添加新字段
  • 除非你
    'domain' => null,
    
    'domain' => 'mysite.com' 
    
    public function redirectToGoogle(Request $request)
    {
        /**
         * @var GoogleProvider $googleDriver
         */
        $googleDriver = Socialite::driver("google");
        return $googleDriver->redirect();
    }
    
    public function fromGoogle(Request $request)
    {
        try {
    /*
    Solution Starts ----
    */
            if (empty($_GET)) {
                $t = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
                parse_str($t, $output);
                foreach ($output as $key => $value) {
                    $request->query->set($key, $value);
                }
            }
    /*
    Solution Ends ----
    */
            /**
             * @var GoogleProvider $googleDriver
             */
            $googleDriver = Socialite::driver("google");
            print_r($googleDriver->user());
        } catch (\Exception $e) {
            print_r($e->getMessage());
        }
    
    $user = Socialite::driver($provider)->stateless()->user();
    
    /*
        |--------------------------------------------------------------------------
        | Same-Site Cookies
        |--------------------------------------------------------------------------
        |
        | This option determines how your cookies behave when cross-site requests
        | take place, and can be used to mitigate CSRF attacks. By default, we
        | do not enable this as other CSRF protection services are in place.
        |
        | Supported: "lax", "strict"
        |
        */
    
        'same_site' => null,
    
    'secure' => env('SESSION_SECURE_COOKIE', true), // or set it to true in your .env file
    
    'same_site' => "none",
    
    location ~ \.(php|phar)(/.*)?$ {
        fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
        fastcgi_intercept_errors on;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_pass   unix:/var/run/php-fpm/www.sock;
    }
    
    location / {
       try_files $uri $uri/ /index.php;
    }
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    …\vendor\laravel\socialite\src\Two\AbstractProvider.php209
    
    $user = Socialite::driver('facebook')->user();
    
    $user = Socialite::driver('facebook')->stateless()->user();