我正在使用Lumen Api& Swift上的OAuth2-Server-Laravel repo和iOS App。
我访问了token
以回复有效的客户端和用户凭据并将其存储在应用中。现在,我正在尝试与token
交换user_id
。
我尝试了OAuth github repo's support getting user_id from access_token
这是我得到的错误:
Fatal error: Call to undefined method LucaDegasperi\OAuth2Server\Facades\Authorizer::getResourceOwnerId() in /home/vagrant/LumenAPI/app/Http/Controllers/OAuthController.php on line 27
我尝试使用token
并收到user_id
作为回报。
我在鲁门的路线看起来像:
$app->get('/me', 'OAuthController@token');
OAuthController:
namespace App\Http\Controllers;
use LucaDegasperi\OAuth2Server\Facades\Authorizer;
class OAuthController extends Controller
{
private $authorizer;
public function __construct(Authorizer $authorizer)
{
$this->authorizer = $authorizer;
}
public function token()
{
$user_id = $this->authorizer->getResourceOwnerId();
return Response::json(['data'=> $user_id]); //Line 27
}
}
我在Swift上使用了这个GET请求:
func me(handler: (data: NSDictionary?, error: String?) -> Void)
{
let url = NSURL(string: "/me", relativeToURL: self.baseUrl)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
// The request is valid only if the access_token exists
if let t = self.getAccessToken() // valid
{
request.setValue("Bearer \(t)", forHTTPHeaderField: "Authorization")
let taskInstance = DataTaskHandler()
taskInstance.make(request, handler: { (result, error) -> Void in
if let res = result
{
if let jsonDictionary = JSONParser(data: res).dictionary()
{
print(jsonDictionary)
handler(data: jsonDictionary, error: nil)
}
}
})
}
else
{
handler(data:nil, error: "The access token is not available")
}
}
答案 0 :(得分:1)
OAuthController应该是
class OAuthController extends Controller
{
public function __construct()
{
$this->middleware('oauth');
}
public function token(Request $request)
{
$user_id = Authorizer::getResourceOwnerId();
return response()->json(['data'=> $user_id]);
}