laravel - 如何使用 Guzzle 以 json 格式返回 Laravel Passport 中的错误

标签 laravel laravel-passport

当密码授予成功时,它返回 json 格式,但当错误时返回 html 格式 Laravel 中的异常 enter image description here

最佳答案

我自己回答了这个问题,将配置添加到 guzzle 'http_errors' => false 将错误返回到 json。

  public function loginapi(Request $request)
{
    $username = $request->username;
    $password =  $request->password;
   $http = new Client;

    $response = $http->post('http://restapi.dev/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 3,
            'client_secret' => 'Lh66IODOP4pZHF676xZA8ghQiIt9OepqYHVzFEIN',
            'username' => $username,
            'password' => $password,
            'scope' => '',
        ],
        'http_errors' => false //add this to return errors in json
    ]);
        return json_decode((string) $response->getBody(), true);
}

关于laravel - 如何使用 Guzzle 以 json 格式返回 Laravel Passport 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43423214/

相关文章:

php - 同一台服务器上的两个 Laravel 应用程序相互冲突

Laravel 迁移创建用户 "unexpected ' use' (T_USE)"SQLite

php - 有没有办法非对称加密使用 Laravel Passport 创建的访问 token ?

php - 使用 Laravel Passport 获取经过身份验证的用户并授予密码

php - Laravel Form Builder 字符串 '@' 被转换为代码

php - 覆盖log接口(interface)容器绑定(bind)lumen 5.0

没有 oAuth 的 Laravel Passport

php - 路线 :list - 404 error on oauth/token 中缺少 Laravel Passport 路线

php - Laravel 护照 oauth/授权错误

php - 在扩展布局的 View 中无法访问 Laravel View Composer 变量