首页 > 解决方案 > 使用 Guzzle 进行身份验证时凭据无效

问题描述

我正在尝试使用 guzzle 制作一个身份验证系统,但结果却抛出了这个错误:

Client error: `POST http://example.com/api/login` resulted in a `401 Unauthorized` response: {"error":"invalid_credentials"}

我已经在 Postman 中使用正确的键和值检查了 API url,并且得到了正确的响应:

{
    "success": true,
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEwMCwiaXNzIjoiaHR0cDovL2Rldi50b2tvaGFqaS5jby5pZC9hcGkvbG9naqwegzxvhyXQiOjE1OTE1NDU3NTUsImV4cCIasdqwegfxdzc6MTU5Mjc1NTM1NSwibmJmIjoxNTkxNTQ1NzU1LCJqdGkiOiJ2QmpZVHhaYXBYNjNvb21XIn0.RAKbWMSjk6I20LxczS9TFEUKV3f7t_fg84q89nhavN8"
}

这是我的控制器:

public function postLogin(Request $request)
    {
        request()->validate([
        'login' => 'required',
        'password' => 'required',
        ]);

        $client = new Client;
        $response = $client->post('http://example.com/api/login', [
            'form-data' => [
               'login' => $request->login, 
               'password' => $request->password
            ]
        ]);

        if (Auth::attempt($response)) {

            if (!empty (Auth::users())) {

                $events = $response['success'];
                foreach($events as $item)
                {
                   DB::table('users')->insert(['phone'=>$item['phone'], 'password'=>$item['password']]);
                   return redirect()->intended('/');
                }

            } else {
                // Authentication passed...
                return redirect()->intended('/');
            }
        }

        return Redirect::to('ecommerce.login')->withSuccess('Invalid credentials');

    }

说明:这里我试图将值发布到 API url,如果成功并且数据库没有关于它的数据,那么用户登录数据(电话号码和密码)将被保存到应用程序数据库并且用户将被登录, else = 用户将立即进入主页。如果失败,用户将返回登录页面。

这是我的路线:

Route::get('/login', 'Ecommerce\FrontController@login')->name('front.login');
Route::post('post-login', [ 'as' => 'post-login', 'uses' => 'Ecommerce\LoginController@postLogin']);

这是我的刀片:

<form action="{{ route('post-login') }}" method="post">
      @csrf
      <div class="container">
      <div class="text-center" style="color:#00843D;">
           <p style="padding-top:20px;">Welcome</p>
      </div>
      <hr>

      <div class="form-group">

           <input class="form-control {{ $errors->has('login') ? ' is-invalid' : '' }}" type="text" name="login" placeholder="Phone Number" value="{{ old('login') }}" autofocus required>

      </div>

      <div class="form-group">

           <input class="form-control {{ $errors->has('login') ? ' is-invalid' : '' }}" type="password" name="password" placeholder="Password" required>

      </div>
      <div class="form-group">

           @if (count($errors) > 0)
               <div class="alert alert-danger">
               <ul>
               @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
               @endforeach
               </ul>
               </div>
           @endif

           <button class="registerbtn" type="submit">Login</button>
      </div>

      </div>

      <div class="container signin" style="padding-bottom:30px;">
           <p>Doesn't have account? <a href="{{ route('front.register') }}">Register in here</a>.</p>
      </div>
</form>

我一直在尝试解决它很长时间,但仍然没有找到答案。希望你能帮助我,谢谢。

编辑:这是邮递员的图片

标签: laravel

解决方案


你需要先得到身体。

后:

$response = $client->post('http://example.com/api/login', [
    'form-data' => [
        'login' => $request->login, 
        'password' => $request->password
    ]
]);

$json = json_decode((string) $response->getBody());

就在那时,您可以使用响应中的 json。


推荐阅读