laravel 5.6 passport .Postman 加载无输出

laravel 5.6 passport .Postman loading no output

我正在尝试构建一个身份验证端点,当用户使用 Laravel 5.6 进行身份验证时,该端点会返回用户的密钥。

当使用 localhost:8000Postman 上进行测试时,我发现它接受请求但无法输出任何内容。 please click here to see the image .

看看下面的AuthController

    <?php

    namespace App\Http\Controllers\Api;

    use App\User;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use GuzzleHttp\Exception\GuzzleException;
    use GuzzleHttp\Client;
    use Hash;

    class AuthController extends Controller
    {
    public function register(Request $request)
    {

        $request->validate([
            'email' => 'required',
            'name' => 'required',
            'password' => 'required'
        ]);

        $user = User::firstOrNew(['email' => $request->email]);

        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();

        $http = new Client;

        $response = $http->post(url('oauth/token'), [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => '2',
                'client_secret' =>'5G7yDJFNDsqzVNSJU85ff8DWW6EiKFLGXDDmMmt9',
                'username' => $request->email,
                'password' => $request->password,
                'scope' => '',
            ],
        ]);

    return response(['data'=>json_decode((string)$response->getBody(),true)]);

    }
    public function login(Request $request)
    {

        $request->validate([
            'email' => 'required',
            'password' => 'required'
        ]);
        $user = User::where('email', $request->email)->first();
        if (!$user) {
       return response(['status' => 'error', 'message' => 'user not found']);
        }
        if (Hash::check($request->password, $user->password)) {

            $http = new Client;

            $response = $http->post(url('oauth/token'), [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => '2',
              'client_secret' => 'JhzSRlU6dnJxI1vb8MpWWksjaOo3AdyuL3Mm6ANf',
                    'username' => $request->email,
                    'password' => $request->password,
                    'scope' => '',
                ],
            ]);

        }
    }
    }

这是用户模型的代码

    <?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

这是api

的代码
    <?php

use Illuminate\Http\Request;


Route::post('/register', 'Api\AuthController@register');
Route::post('/login', 'Api\AuthController@login');

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

这里的第一件事是,您可以按如下方式更改邮递员请求,

  1. 添加Headers如下。
  2. 将 Body 添加为 form-data

最重要的是检查您的端口是否正确,laravel 服务器是 运行。默认端口是端口 8000。然后你的 url 应该形成为

http://localhost:8000/api/register (note that this url is only an example format)

尝试进行上述更改并向我们提供您所获得的结果。认为这可能有所帮助。

谢谢

问题是在使用 php artisan serve 时,它使用 PHP 服务器,即 single-threaded

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

你可以这样做solution:

When making calls to itself the thread blocked waiting for its own reply. The solution is to either seperate the providing application and consuming application into their own instance or to run it on a multi-threaded webserver such as Apache or nginx.

或者,如果您正在寻找快速修复来测试您的更新 - 您可以通过打开两个命令提示符来完成此操作。第一个是 运行ning php artisan serve(本地我的默认端口是 8000,您将 运行ning 您的网站 http://localhost:8000)。第二个会 运行 php artisan serve --port 8001.

然后您将 post 请求更新为:

    $response = $http->post('http://localhost:8001/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => '2',
            'client_secret' =>'5G7yDJFNDsqzVNSJU85ff8DWW6EiKFLGXDDmMmt9',
            'username' => $request->email,
            'password' => $request->password,
            'scope' => '',
        ],
    ]);

这应该会在您的测试期间有所帮助,直到您能够访问服务器或本地虚拟主机上的所有内容。