lucadegasperi oauth2-server with Auth Code Grant给出invalid_client错误

时间:2014-10-22 09:16:45

标签: laravel oauth-2.0

我正在使用来自::

的oauth2-server
 https://github.com/lucadegasperi/oauth2-server-laravel 

我已使用Auth Code Grant ::

实现
 github.com/lucadegasperi/oauth2-server-laravel/wiki/Implementing-an-Authorization-Server-with-the-Auth-Code-Grant 

现在因为我是oauth2的新手,我试图使用::

访问数据
localhost.com/oauth/authorize?response_type=code&client_id=client1id&redirect_uri=https://www.mysite.com

但作为回应我有

{"error":"invalid_client","error_description":"Client authentication failed."}

修改
Route.php

  <?php
 Route::get('/', function()
 {
   return View::make('hello');
 });

Route::group(['prefix' => 'api/v1'], function()
{
    Route::resource('API', 'APIController');
});

Route::get('oauth/authorize', ['before' => 'check-authorization-params|auth', function() {
  View::make('oauth/authorization-form', Authorizer::getAuthCodeRequestParams());
}]);

Route::post('oauth/authorize', ['before' => 'csrf|check-authorization-params|auth', function() {
  $params['user_id'] = Auth::user()->id;
  $redirectUri = '';
  if (Input::get('approve') !== null) {
      $redirectUri = Authorizer::issueAuthCode('user', $params['user_id'], $params);
  }

  if (Input::get('deny') !== null) {
      $redirectUri = Authorizer::authCodeRequestDeniedRedirectUri();
  }

  return Redirect::to($redirectUri);
}]);

Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
});

控制器/ OAuthController.php

<?php

use Illuminate\Routing\Controller;
use LucaDegasperi\OAuth2Server\Authorizer;

class OAuthController extends Controller
{
protected $authorizer;

public function __construct(Authorizer $authorizer)
{
    $this->authorizer = $authorizer;

    $this->beforeFilter('auth', ['only' => ['getAuthorize', 'postAuthorize']]);
    $this->beforeFilter('csrf', ['only' => 'postAuthorize']);
    $this->beforeFilter('check-authorization-params', ['only' => ['getAuthorize', 'postAuthorize']]);
}

public function postAccessToken()
{
     return Response::json($this->authorizer->issueAccessToken());
}

public function getAuthorize()
{
    return View::make('authorization-form', $this->authorizer->getAuthCodeRequestParams());
}

public function postAuthorize()
{
    // get the user id
    $params['user_id'] = Auth::user()->id;

    $redirectUri = '';

    if (Input::get('approve') !== null) {
        $redirectUri = $this->authorizer->issueAuthCode('user', $params['user_id'], $params);
    }

    if (Input::get('deny') !== null) {
        $redirectUri = $this->authorizer->authCodeRequestDeniedRedirectUri();
    }

    return Redirect::to($redirectUri);
  }
}

的oauth2 - 服务器 - laravel \ oauth2.php     

'database' => 'default',

'grant_types' => [
    'authorization_code' => [
        'class' => '\League\OAuth2\Server\Grant\AuthCodeGrant',
        'access_token_ttl' => 3600,
        'auth_code_ttl'   => 3600
    ]
],

'token_type' => 'League\OAuth2\Server\TokenType\Bearer',

'state_param' => false,

'scope_param' => false,

'scope_delimiter' => ',',

'default_scope' => 'oauth_scopes' ,

'access_token_ttl' => 3600,

'limit_clients_to_grants' => false,

'limit_clients_to_scopes' => false,

'limit_scopes_to_grants' => false,

'http_headers_only' => false,
];

1 个答案:

答案 0 :(得分:2)

在对服务器代码进行一些挖掘之后,我发现check-authorization-params路由过滤器检查redirect_uri表中是否存在oauth_client_endpoints(该表与{{{ 1}}表)。

因此,您需要oauth_clients中的redirect_uri与所需的oauth_client_endpoints一致。