I accessed to the access_token successfully, however I'm having problem getting the user_id. I am getting error:
NoActiveAccessTokenException Authorizer.php line 104 Tried to access session data without an active access token
What am I doing wrong?
$app->get('/me', function () {
$id = Authorizer::getResourceOwnerId();
return response()->json(['id' => $id]);
});
On Swift (note that token
is valid):
func me(handler: (data: NSDictionary?, error: String?) -> Void)
{
let url = NSURL(string: "/me", relativeToURL: self.baseUrl)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
if let t = self.getAccessToken()
{
request.setValue("Bearer \(t)", forHTTPHeaderField: "Authorization")
print(t) // Token is valid "y7bPb1NgUBxmHSIbwsgBgPxJcM0ywnithG5PvEEv"
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")
}
}
Take into account that you access_token is only valid for shor time. SO after that time, the access token must be refreshed, using the refresh_toke, grant_type. Check here the details: https://github.com/lucadegasperi/oauth2-server-laravel/wiki/Implementing-an-Authorization-Server-with-the-Refresh-Token-Grant
Alternatively you can increase the time for the access_token. Just go to config/oauth.php and change the ttl index (normally is 3600 -> 1 hour), please take into account that one access_token must have shor time for security reasons, so dot increase too much the ttl.
EDIT:
It is importan to check that you are using the oauth middleware in the controller method or during the request, because this middleware stablish the access_token value during the whole request. Just add this in the constructor of your controller.
public function __construct()
{
$this->middleware('oauth');
}