簡體   English   中英

使用 Guzzle 進行身份驗證時憑據無效

[英]Invalid Credentials When Using Guzzle to make an Authentication

我正在嘗試使用 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"
}

這是我的 Controller:

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 = 用戶將立即 go 到主頁。 如果失敗,用戶將返回登錄頁面。

這是我的路線:

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>

我一直在嘗試解決它很長時間,但仍然沒有找到答案。 希望你能幫助我,謝謝。

編輯:這是來自 Postman 的圖像

你需要先得到身體。

后:

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

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

就在那時,您有 json 從您的響應中使用。

我解決了!

因此,我嘗試查找有關 API 是否在完整 url 上的外觀的一些參考,它引導我找到答案!

在這里,我將我的 Controller 更改為:

$login = $request->login;
$password = $request->password;

$client = new Client;
$response = $client->post('http://example.com/api/login?login='.$login.'&password='.$password);

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

謝謝@JoeGalind 幫助我::)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM