我正在使用 Laravel 5.4 和 Passport 创建一个 API。
API 授权是使用密码授权类型完成的。
下面是一个示例请求:
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'taylor@laravel.com',
'password' => 'my-password',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
这将使用以下响应向“/oauth/token”发送 POST 请求:
{
"token_type": "Bearer",
"expires_in": 3155673600,
"access_token": "eyJ0eXAiOiJK...",
"refresh_token": "LbxXGlD2s..."
}
我想要的是得到一个响应,包括经过身份验证的用户,如下所示:
[
data:{
name: Jhon,
email: jhon@example.com,
address: ASDF Street no.23
},
token{
"token_type": "Bearer",
"expires_in": 3155673600,
"access_token": "eyJ0eXAiOiJK...",
"refresh_token": "LbxXGlD2s..."
}
]
我已经做的是更改第 65 行的 PasswordGrant 文件
vendor/league/oauth2-server/src/Grant/PasswordGrant.php
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);
return $responseType;
我希望有人可以帮助我,并告诉我如何解决这个问题,
谢谢你。
最佳答案
你可以写一个 (after-) middleware它采用原始响应并将其转换为您希望的方式。但请注意,所有其他 Passport 中间件( CheckClientCredentials
、 CheckScopes
、 CreateFreshApiToken
等...)可能取决于该特定格式,并且也必须进行调整。
关于laravel - 使用 Passport 和 Laravel 进行身份验证时返回访问 token 和用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43412235/