隐式授予Laravel 5.4护照unsupported_grant_type错误

时间:2017-05-11 06:09:27

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

我已成功使用护照2.0和laravel 5.4实施授权代码授予和密码授予。添加Passport :: enableImplicitGrant()之后;在AuthServiceProvider.php中,我尝试使用angular2 app实现隐式授权。

  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错误

1 个答案:

答案 0 :(得分:0)

在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?$查询

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

oauth / authorize GET请求的处理程序是: Laravel \护照\ HTTP \控制器\ AuthorizationController @授权 根据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);
    }

看起来很熟悉吗?