我已经使用 Passport 2.0 和 Laravel 5.4 成功实现了授权码授予和密码授予。添加 Passport::enableImplicitGrant(); 后在 AuthServiceProvider.php 中,我尝试使用 angular2 应用程序实现隐式授权。
getImplicitAccessToken() {
const headers = new Headers({
'Content-Type': 'application/json',
'Accept' : 'application/json'
});
const query = {
'grant_type' : 'token',
'client_id' : Constants.IMPLICIT_TEST_CLIENT_ID,
'redirect_uri' : window.location.origin + '/implicit-code-grant',
'scope': ''
};
const params = this.getParamsFromJson(query);
window.location.href = Constants.OAUTH_AUTHORIZATION_URL + '?' + params.toString();
}
private getParamsFromJson(query: any) {
const params = new URLSearchParams();
for (const key in query) {
params.set(key, query[key]);
}
return params;
}
但是我收到了 unsupported_grant_type 错误
最佳答案
在 Laravel 5.4 文档中执行隐式授予类型时 答案:
为什么隐式授予不起作用
按照教程的结果是:
// 20170711152854
// http://oauth2server1/oauth/authorize?KEY=14997536295521&client_id=1&redirect_uri=http%3A%2F%2Fauthorizationgrantclient1%2Fcallback&response_type=token&scope=%3FXDEBUG_SESSION_START%3DECLIPSE`enter code here`_DBGP
{
"error": "unsupported_grant_type",
"message": "The authorization grant type is not supported by the authorization server.",
"hint": "Check the `grant_type` parameter"
}
============================================================================================
在隐式授予 token 请求代码中,它发出请求: http://oauth2server1/oauth/authorize ?$查询
============================================================================================
oauth/authorize GET 请求的处理程序是: Laravel\Passport\Http\Controllers\AuthorizationController@authorize 根据 php artisan 路线:列表
============================================================================================
...某处
============================================================================================
In vendor\league\oauth2-server\src\AuthorizationServer.php -> function validateAuthorizationRequest()
/**
* Validate an authorization request
*
* @param ServerRequestInterface $request
*
* @throws OAuthServerException
*
* @return AuthorizationRequest
*/
public function validateAuthorizationRequest(ServerRequestInterface $request)
{
foreach ($this->enabledGrantTypes as $grantType)
{
if($grantType->canRespondToAuthorizationRequest($request)) // <— ValidationStartsHere
{
return $grantType->validateAuthorizationRequest($request);
}
}
throw OAuthServerException::unsupportedGrantType();
}
============================================================================================
…在某处
============================================================================================
In vendor/league/oauth2-server/src/Grant/AuthCodeGrant.php -> function canRespondToAuthorizationRequest()
/**
* {@inheritdoc}
*/
public function canRespondToAuthorizationRequest(ServerRequestInterface $request)
{
return (array_key_exists('response_type', $request->getQueryParams()) // TRUE
&& $request->getQueryParams()['response_type'] === 'code' // FALSE
&& isset($request->getQueryParams()['client_id']) // TRUE
);
}
the values of the following variables are as follows:
$request->getQueryParams():
“KEY” => “14997536295521”,
“client_id” => “1”,
“redirect_uri” => “http://authorizationgrantclient1/callback”, // refer this value back to how to make an implicit grant token request
“response_type” => “token”,
“scope” => “”
作为效果...此代码始终返回 false,并且代码执行返回到调用函数
============================================================================================
going back to vendor\league\oauth2-server\src\AuthorizationServer.php->validateAuthorizationRequest()
/**
* Validate an authorization request
*
* @param ServerRequestInterface $request
*
* @throws OAuthServerException
*
* @return AuthorizationRequest
*/
public function validateAuthorizationRequest(ServerRequestInterface $request)
{
foreach ($this->enabledGrantTypes as $grantType) {
if ($grantType->canRespondToAuthorizationRequest($request)) {
return $grantType->validateAuthorizationRequest($request);
}
}
throw OAuthServerException::unsupportedGrantType(); // <—looks familiar?
}
============================================================================================
…某处
============================================================================================
In vendor\league\oauth2-server\src\Exception\OAuthServerException.php->function unsupportedGrantType()
/**
* Unsupported grant type error.
*
* @return static
*/
public static function unsupportedGrantType()
{
$errorMessage = 'The authorization grant type is not supported by the authorization server.';
$hint = 'Check the `grant_type` parameter';
return new static($errorMessage, 2, 'unsupported_grant_type', 400, $hint);
}
看起来非常熟悉对吧?
关于php - 隐式授予 Laravel 5.4 护照 unsupported_grant_type 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43907749/