由另一个(外部)Laravel 项目使用 Laravel API
Use Laravel API by another (external) Laravel project
我想使用我的第一个 Laravel 实例的 API-路由(我将调用此 Laravel API 提供程序) 我的第二个实例(我将调用此 Laravel API 客户端)。
Laravel API 提供商 基于 vue/vuex/vue-router 并且 API-路由受 laravel/passport
.
Laravel API 提供商:
上受保护路由的一个示例
/*Categories Routes*/
Route::group(['prefix' => 'categories'], function ($router) {
/*Index*/
Route::middleware('auth:api')->get('/', 'CategoriesApiController@index')
->name('api.categories.index');
});
所以现在我在 Laravel API 客户端上创建了这个调用:
$http= new Client();
$response = $http->request('POST', 'https://laravel-api-provider.local/oauth/token', [
'headers' => [
'cache-control' => 'no-cache',
'Content-Type' => 'application/x-www-form-urlencoded'
],
'form_params' => [
'client_id' => '2',
'client_secret' => 'secret',
'grant_type' => 'password',
'username' => 'test@example.org',
'password' => 'password',
],
]);
return json_decode((string) $response->getBody(), true);
这个returns:
{
"token_type": "Bearer",
"expires_in": 31622400,
"access_token": "eyJ0eXAiOiJKV1QiLC......",
"refresh_token": "def5020084262c0659e6f916b4da2c33e2a78de2206d......"
}
看起来不错。所以我的下一个问题是:如何在 Laravel API 客户端 上使用 $response
调用受保护的路由(如 /api/categories/index
)?
Passport 使用不记名令牌,这是在 Authorization
header 中设置的。令牌前面应该有'Bearer '
。所以你可以用这样的东西来实现它。
$token = $response['access_token'];
$http= new Client();
$response = $http->request('GET', 'https://laravel-api-provider.local/api/categories/index', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
为了获得最佳使用效果,请存储令牌,当调用不再被授权时使用刷新令牌获取新令牌。但现在这应该会让你朝着正确的方向前进。
我想使用我的第一个 Laravel 实例的 API-路由(我将调用此 Laravel API 提供程序) 我的第二个实例(我将调用此 Laravel API 客户端)。
Laravel API 提供商 基于 vue/vuex/vue-router 并且 API-路由受 laravel/passport
.
Laravel API 提供商:
上受保护路由的一个示例/*Categories Routes*/
Route::group(['prefix' => 'categories'], function ($router) {
/*Index*/
Route::middleware('auth:api')->get('/', 'CategoriesApiController@index')
->name('api.categories.index');
});
所以现在我在 Laravel API 客户端上创建了这个调用:
$http= new Client();
$response = $http->request('POST', 'https://laravel-api-provider.local/oauth/token', [
'headers' => [
'cache-control' => 'no-cache',
'Content-Type' => 'application/x-www-form-urlencoded'
],
'form_params' => [
'client_id' => '2',
'client_secret' => 'secret',
'grant_type' => 'password',
'username' => 'test@example.org',
'password' => 'password',
],
]);
return json_decode((string) $response->getBody(), true);
这个returns:
{
"token_type": "Bearer",
"expires_in": 31622400,
"access_token": "eyJ0eXAiOiJKV1QiLC......",
"refresh_token": "def5020084262c0659e6f916b4da2c33e2a78de2206d......"
}
看起来不错。所以我的下一个问题是:如何在 Laravel API 客户端 上使用 $response
调用受保护的路由(如 /api/categories/index
)?
Passport 使用不记名令牌,这是在 Authorization
header 中设置的。令牌前面应该有'Bearer '
。所以你可以用这样的东西来实现它。
$token = $response['access_token'];
$http= new Client();
$response = $http->request('GET', 'https://laravel-api-provider.local/api/categories/index', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
为了获得最佳使用效果,请存储令牌,当调用不再被授权时使用刷新令牌获取新令牌。但现在这应该会让你朝着正确的方向前进。