Laravel护照GuzzleHttp错误API没有数据

时间:2018-06-30 01:56:02

标签: laravel api vue.js

我使用GuzzleHtpp和护照通过vue.js进行登录页面创建,并且在以下API URL中出现了Insomnia API中的问题:http://ledger.work/api/login

这是我的AuthController:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    public function login (Request $request)
    {
        $http = new \GuzzleHttp\Client;
        try {
            $response = $http->post('http://ledger.work/oauth/token', [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => 2,
                    'client_secret' => 'f7peunlDCOgUK1qEK0HP4267wtlb6zTkjxLshvBj',
                    'username' => $request->username,
                    'password' => $request->password,
                ]
            ]);
            return $response->getBody();
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
            if ($e->getCode() === 400) {
                return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
            } else if ($e->getCode() === 401) {
                return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
            }
            return response()->json('Something went wrong on the server.', $e->getCode());
        }


    }
}

这是我的路线

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

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


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

Route::get('/users', 'UsersController@index');

Its just loading and got no thing

Insomnia API刚刚加载,什么也没有解决。

1 个答案:

答案 0 :(得分:1)

这是因为php artisan serve不能正常使用。在本地域或诸如此类(XAMPP)下使用xampp。