开发者问题收集

传递给 Symfony\Component\HttpFoundation\Response::setContent() 的参数 1 必须是字符串类型或 null,给定对象

2021-02-24
11588

我尝试使用 Laravel 7 和软件包 (Socialite) 通过 Facebook 注册,当按下 URL 时:

<div class="form-group row mb-0">
     <div class="col-md-6 offset-md-4">
          <a href="{{url('redirect/facebook')}}">login with your facebook account</a>
     </div>
</div>

重定向页面运行应用程序,我在 Facebook 开发人员中创建为图片:

在此处输入图片说明

当按下(继续作为 Mahmoud)并输入我的电子邮件并传递此错误时显示:

TypeError 传递给 Symfony\Component\HttpFoundation\Response::setContent() 的参数 1 必须是字符串类型或 null,给定对象,在 C:\wamp64\www\Starter\vendor\laravel\framework\src\Illuminate\Http\Response.php 上行调用65

并在 chrom 框上方添加以下 URL:

http://localhost:8000/callback/facebook?code=AQDlnu6qDKxTV6nG_vN4DYMKSyVP-zEKTL_uJDKbENBTc-pBwtIxpLRkcUS_zebL3nbPo8RjKKC3D_UbZkHM8dz5iZU8c4-PPPdyS_teoLZZ77kVf1pLGCx0ggTwx0EJys8nK3aPE8BugkbgcXTFUt7qcDxQcX1ptlmG9_0qSbuifaEg 678bl71Gbm3qU5Jx- ZsonmuadY3kHTbq8vG-3s2jqV4KkxDSCw-m-4LrIe46v73EXFzG1wFNMbkWB4MJay5ZewPjI2ah3p8o5FCgGLp1Rs4clsKW5B2fQyoWc6MM-Ut2jESoQ7YMOjZpuX5kA1aWP3To1TncAjKpWsAevIa&state=n0XP5Kf68IZlOUqm1pwUYryb9md9nHCdSnatvbpO# =_

我的路线:

Route::get('/redirect/{service}','SocialiteController@redirect');
Route::get('/callback/{service}','SocialiteController@callback');

控制器:

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;

class SocialiteController extends Controller
{
    public function redirect($service){
        return Socialite::driver($service)->redirect();

    }
    public function callback($service){

         $user = Socialite::with($service) -> user();
         return $user;

    }

}

.env:

FB_CLIENT_ID = FB_ID
FB_CLIENT_SECRET = FB_secret
FB_REDIRECT = 'http://localhost:8000/callback/facebook'

config/services.php:

 'facebook' => [
    'client_id' => env ('FB_CLIENT_ID'),
    'client_secret' => env ('FB_CLIENT_SECRET'),
    'redirect' => env ('FB_REDIRECT')
],

在 config/app.php 中我添加了:

Laravel\Socialite\SocialiteServiceProvider::class,

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

请问可以帮帮我吗,我尝试了很多方法但都没用。

2个回答

我应该返回一个 Response 而不是控制器中的 $user 对象

public function callback($service){

     $user = Socialite::with($service) -> user();
     return $user;

}

更改为

        public function callback($service){
           $user = Socialite::with($service)->stateless()->user();
           return response()->json($user);
    
        }
Mahmoud Diab
2021-02-24

尝试:

public function callback($service){
    $user = Socialite::with($service) -> user();
    return (array)$user;
}
killlinuxkill
2021-06-18