public function getUserAccessTokenAction(Request $request, $clientId)
{
$response = new Response();
$session = $this->container->get('session');
$clientManager = $this->container->get('fos_oauth_server.client_manager.default');
$client = $clientManager->findClientByPublicId($clientId);
if (!$client instanceof Client) {
throw new NotFoundHttpException("Client {$clientId} is not found.");
}
$this->container->get('user')->getCurrentUser();
$redirectUris = $client->getRedirectUris();
$authUrl = $request->getUriForPath('/oauth/v2/auth');
$tokenUrl = $request->getUriForPath('/oauth/v2/token');
// GET to get code
$authRequest = Request::create($authUrl, 'GET', array('client_id' => $clientId, 'redirect_uri' => $redirectUris[0], 'response_type' => 'code'), $request->cookies->all());
$kernel = $this->get('http_kernel');
$codeResponse = $kernel->handle($authRequest);
$locationHeader = $codeResponse->headers->get('Location');
$code = substr($locationHeader, strpos($locationHeader, "code=") + 5);
if ($codeResponse->getStatusCode() !== 302) {
$codeResponse->setStatusCode(401);
return $codeResponse;
}
//make a GET to get token
$tokenRequest = Request::create($tokenUrl, 'GET', array("client_id" => $clientId, "client_secret" => $client->getSecret(), "redirect_uri" => $redirectUris[0], "code" => $code, "grant_type" => "authorization_code"));
$tokenResponse = $kernel->handle($tokenRequest, HttpKernelInterface::SUB_REQUEST);
return json_decode($tokenResponse->getContent(), true);
}