Laravel oAuth2服务器获取访问令牌数据

时间:2013-12-26 03:21:53

标签: php json laravel

我的代码:

        $user = Auth::user();
        $access_token = AuthorizationServer::performAccessTokenFlow();

        var_dump($access_token);

        return Response::json(array(
            'id' => $user->uid,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'email' => $user->email,
            'avatar' => $user->avatar_id,
            'cover' => $user->cover_id,
            'access' => $access_token
        ));

问题是json响应中的$ access_token只显示HEADERS而没有别的。

var_dump返回:

object(Illuminate\Http\JsonResponse)#409 (8) {
  ["data":protected]=>
  string(147) "{
    "access_token": "CDkW0mlsh6TO01iLr2t0hMEvhlKqii4S7jfRY6AW",
    "token_type": "bearer",
    "expires": 1388632702,
    "expires_in": 604800
}"
  ["callback":protected]=>
  NULL
  ["headers"]=>
  object(Symfony\Component\HttpFoundation\ResponseHeaderBag)#413 (5) {
    ["computedCacheControl":protected]=>
    array(1) {
      ["no-cache"]=>
      bool(true)
    }
    ["cookies":protected]=>
    array(0) {
    }
    ["headerNames":protected]=>
    array(3) {
      ["cache-control"]=>
      string(13) "Cache-Control"
      ["date"]=>
      string(4) "Date"
      ["content-type"]=>
      string(12) "Content-Type"
    }
    ["headers":protected]=>
    array(3) {
      ["cache-control"]=>
      array(1) {
        [0]=>
        string(8) "no-cache"
      }
      ["date"]=>
      array(1) {
        [0]=>
        string(29) "Thu, 26 Dec 2013 03:18:22 GMT"
      }
      ["content-type"]=>
      array(1) {
        [0]=>
        string(16) "application/json"
      }
    }
    ["cacheControl":protected]=>
    array(0) {
    }
  }
  ["content":protected]=>
  string(147) "{
    "access_token": "CDkW0mlsh6TO01iLr2t0hMEvhlKqii4S7jfRY6AW",
    "token_type": "bearer",
    "expires": 1388632702,
    "expires_in": 604800
}"
  ["version":protected]=>
  string(3) "1.0"
  ["statusCode":protected]=>
  int(200)
  ["statusText":protected]=>
  string(2) "OK"
  ["charset":protected]=>
  NULL
}

但我想得到的只是数据。如果数据受到保护,我该怎么做?

object(Illuminate\Http\JsonResponse)#409 (8) {
["data":protected]=>
  string(147) "{
    "access_token": "CDkW0mlsh6TO01iLr2t0hMEvhlKqii4S7jfRY6AW",
    "token_type": "bearer",
    "expires": 1388632702,
    "expires_in": 604800
}"

我使用的是:https://github.com/lucadegasperi/oauth2-server-laravel/

1 个答案:

答案 0 :(得分:1)

Illuminate \ Http \ JsonResponse有getData()方法,所以你可以这样做:

$access_token = AuthorizationServer::performAccessTokenFlow()->getData()->access_token;