我正在使用Oauth-server-laravel身份验证。
当我向在laravel中创建的API发布错误access_token
时,它会给出以下回复,
{
"error": "access_denied",
"error_description": "The resource owner or authorization server denied the request."
}
我在路由中使用oauth
作为中间件,如下所示,
Route::group(['namespace' => 'Modules\User\Http\Controllers', 'middleware' => 'oauth'], function () {
// Get User Profile Details
Route::post('getUserProfileDetail', 'UserController@getUserProfileDetail');
});
如果凭据有误,那么oauth会自动回复它的默认消息,我想自定义该响应消息,
我有一半成功,如果凭据是正确的,那么它调用在路由中指定的函数,并且我追加我要发送的mre响应。
$response = $this->authorizer->issueAccessToken();
$code = 200; //Response OK
$response['result'] = 'success';
$response['user_id'] = $user['id'];
$response['email'] = $user['email'];
$response['name'] = $user['name'];
但是,如果凭据不正确,我无法发送凭据,因为它无法调用功能并将其发送给用户。
答案 0 :(得分:1)
我将这类问题设置为自定义消息然后this worked for me(我写的关于这篇文章的博客文章)]
首先在app/Http/Middleware.
中创建一个中间件我的中间件名称为OauthExceptionMiddleware
然后打开
app/Http/kernel.php
并在oauth2
数组中放置此中间件而不是$middleware
以前的中间件,就像这样
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\OauthExceptionMiddleware::class,
];
<?php
/**
* Created by PhpStorm.
* User: kingpabel
* Date: 3/23/16
* Time: 4:40 PM
*/
namespace app\Http\Middleware;
use Closure;
use League\OAuth2\Server\Exception\OAuthException;
class OauthExceptionMiddleware
{
public function handle($request, Closure $next)
{
try {
$response = $next($request);
// Was an exception thrown? If so and available catch in our middleware
if (isset($response->exception) && $response->exception) {
throw $response->exception;
}
return $response;
} catch (OAuthException $e) {
$data = [
// 'error' => $e->errorType,
// 'error_description' => $e->getMessage(),
'error' => 'Custom Error',
'error_description' => 'Custom Description',
];
return \Response::json($data, $e->httpStatusCode, $e->getHttpHeaders());
}
}
}