jos*_*son 0 curl oauth laravel-passport laravel-5.6
我已经使用 Laravel Passport 安装设置了一个新的 Laravel 5.6 安装。如果我使用 Postman向http://127.0.0.1/oauth/token发出帖子请求,我会得到一个有效的令牌:
要求
POST /oauth/token HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Cache-Control: no-cache
Postman-Token: 99529c07-0fe3-38a8-54cf-8b80a9dd5fbd
{
"grant_type" : "password",
"client_id" : 4,
"client_secret" : "Ib1UOS7BK12tFxOilqwea1XGJhrExbVYe8B7r8wK",
"username" : "mail@mail.com",
"password" : "password"
}
Run Code Online (Sandbox Code Playgroud)
回复:
{
"token_type": "Bearer",
"expires_in": 31536000,
"access_token": "eyJ0eXAiOiJKV1.....",
"refresh_token": "def5020026dfeb6f91f6a9....."
}
Run Code Online (Sandbox Code Playgroud)
我不希望我的用户直接访问它,所以我在 routes/web.php 文件中设置了一个路由:
Route::post('login', 'API\AuthController@login');
Run Code Online (Sandbox Code Playgroud)
我的 AuthController 看起来像:
<?php
namespace App\Http\Controllers\API;
use App\OAuth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\User;
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Login user and create token
*
* @param [string] email
* @param [string] password
* @param [boolean] remember_me
* @return [string] access_token
* @return [string] token_type
* @return [string] expires_at
*/
public function login(Request $request)
{
$request->validate([
'username' => 'required|string|email',
'password' => 'required|string'
]);
$credentials = request(['username', 'password']);
return OAuth::login($credentials['username'], $credentials['password']);
}
Run Code Online (Sandbox Code Playgroud)
这将调用我的 OAuth 类的登录方法:
namespace App;
use GuzzleHttp;
class OAuth
{
public static function login($username, $password)
{
$http = new GuzzleHttp\Client;
$response = $http->post('http://127.0.0.1:8000/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 4,
'client_secret' => 'Ib1UOS7BK12tFxOilqwea1XGJhrExbVYe8B7r8wK',
'username' => $username,
'password' => $password
],
]);
return json_decode((string) $response->getBody(), true);
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用 Postman 发出 post 请求以获取令牌时:
POST /login HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Cache-Control: no-cache
Postman-Token: 94469bd8-a011-7e04-5f20-b835a5b1cac4
{
"username" : "mail@mail.com",
"password" : "password"
}
Run Code Online (Sandbox Code Playgroud)
请求只是挂起。它不会返回任何类型的响应或超时,它似乎永远加载。我不确定出了什么问题。如果我从邮递员那里向 oauth/token 发出 post 请求,它会起作用,但是如果我向控制器方法发出 post 请求,而控制器方法又向 oauth/token 发出 post 请求,则它不起作用。我什么也得不到。
| 归档时间: |
|
| 查看次数: |
2650 次 |
| 最近记录: |