【问题标题】:Invalid Credentials When Using Guzzle to make an Authentication使用 Guzzle 进行身份验证时凭据无效
【发布时间】:2020-06-09 23:26:31
【问题描述】:

我正在尝试使用 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,如果成功并且数据库没有关于它的数据,那么用户登录数据(电话号码和密码)将被保存到应用程序数据库并且用户将是登录,否则=用户将立即进入主页。如果失败,用户将返回登录页面。

这是我的路线:

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>

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

编辑:Here is the image from Postman

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你需要先得到身体。

    之后:

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

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

    【讨论】:

    • 感谢您的回复。它仍然抛出相同的错误:“客户端错误:POST http://example.com/api/login 导致401 Unauthorized 响应:{“error”:“invalid_credentials”}“我认为它与 $response 有关,但我不知道它是什么,我已将“表单数据”更改为“身份验证”,如 guzzle 文档中所述,但结果是:“未定义偏移量:0”。
    • 您是如何从 Postman 发送信息的?请分享屏幕截图以查看您使用的正确配置
    • 我已经在帖子中添加了它,抱歉我不能给你确切的网址。没有授权/标题/预请求脚本/测试,只有正文。
    • 你用的是什么版本的 Guzzle?
    • 我认为它是最新的,即版本 7
    【解决方案2】:

    我解决了!

    所以我尝试找到一些关于如果 API 在完整 url 上的外观的参考,它会引导我找到答案!

    在这里,我将我的控制器更改为:

    $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 帮助我! :)

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多