【问题标题】:Implicit grant Laravel 5.4 passport unsupported_grant_type error隐式授予 Laravel 5.4 护照 unsupported_grant_type 错误
【发布时间】:2017-10-10 00:05:28
【问题描述】:

我已经用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 错误

【问题讨论】:

  • 我也有这个问题,正在网上找解决办法

标签: php oauth-2.0 laravel-5.4 laravel-passport


【解决方案1】:

在 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"
    }

============================================================================================

在隐式授权令牌请求代码中,它正在向: http://oauth2server1/oauth/authorize?$query

============================================================================================

oauth/authorize GET 请求的处理程序是: Laravel\Passport\Http\Controllers\AuthorizationController@authorize 根据 php artisan route:list

============================================================================================

... 某处下线

============================================================================================

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);
    }

看起来很眼熟吧?

【讨论】:

  • 我想纠正自己,我通过在授权服务器中添加以下行解决了我的问题: 在 AuthServiceProvider.php public function boot() { $this->registerPolicies();护照::路线(); Passport::enableImplicitGrant(); } 以前,我在我的 CLIENT 应用程序中有它们
猜你喜欢
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 2018-09-26
  • 2020-09-08
  • 2018-02-19
  • 2017-12-10
  • 1970-01-01
  • 2018-02-25
相关资源
最近更新 更多