Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL timeout on heroku

I'm using a Symfony2 app on heroku and I can't connect to my API.

I explain you the scenario :

  1. I connect me in my app through a form
  2. When I'm correctly log in my app, an event listener is triggered
  3. I access the URL which delivers me the API token and I save those credentials in the user session
  4. I redirect the connected (to the app and to the API) user to the homepage of the app

So my problem is at the point 3. I tried with cURL and a file_get_contents to access the URL and both aren't worked.

When I try to access the URL which delivers me the OAuth token necessary to access my API, it fails. I tried to access this URL manually in the browser (or Postman) and it returns well the OAuth token.

The event listener :

        // Call of the url to deliver the token
        $clientAPI = $this->em->getRepository('BgAPIBundle:Client')->findOneBy(array('providerAccess' => 'pcv'));
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, urldecode($this->router->generate('fos_oauth_server_token', array('grant_type' => 'http://www.myapp/api/grants/api_key', 'client_id' => $clientAPI->getId() . '_' . $clientAPI->getRandomId(), 'client_secret' => $clientAPI->getSecret(), 'api_key' => $event->getAuthenticationToken()->getUser()->getApiKey()), true)));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_COOKIESESSION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($curl);
        $this->error = $response; // Only if there is problem
        curl_close($curl);

        // Write in logs
        $date = new \DateTime('now');
        if (preg_match('#error#', $this->error)) {
            $this->logger->error('Error on authentication success to deliver the OAuth token for ' . $event->getAuthenticationToken()->getUser() . ' the ' . $date->format('j/m/Y - H:i:s') . ' with message : ' . $this->error);
        }

The error from Heroku and my logs :

heroku/router:  at=error code=H12 desc="Request timeout" method=GET path="/app_dev.php/oauth/v2/token?grant_type=http://www.app/api/grants/api_key&client_id=261_1ws6mvgmbeisgcsw4wokwk8k400og88gs0csssg0gk0884080s&client_secret=1ghmf01c1a4kc448ssgwg8sw04840c4ww8k00gg4o0k8w04g4&api_key=7f5284ac5ec8b35527d3c16dafa52a89" host=app-max.herokuapp.com request_id=dc8960fd-d154-4e5d-bc2f-34d4f25b8070 fwd="46.51.146.244" dyno=web.1 connect=0ms service=30006ms status=503 bytes=0 
app.ERROR: Une erreur est survenue lors de l'attribution du token OAuth de [email protected] le 25/11/2015 - 16:12:40 avec le message : <!DOCTYPE html>     <html>     <head>       <meta name="viewport" content="width=device-width, initial-scale=1">       <style type="text/css">         html, body, iframe { margin: 0; padding: 0; height: 100%; }         iframe { display: block; width: 100%; border: none; }       </style>     <title>Application Error</title>     </head>     <body>       <iframe src="//s3.amazonaws.com/heroku_pages/error.html">         <p>Application Error</p>       </iframe>     </body>     </html> [] []

Thanks for your help !

like image 944
Maxime Picard Avatar asked Nov 27 '15 09:11

Maxime Picard


People also ask

How do I check my curl timeout?

Tell curl with -m / --max-time the maximum time, in seconds, that you allow the command line to spend before curl exits with a timeout error code (28). When the set time has elapsed, curl will exit no matter what is going on at that moment—including if it is transferring data. It really is the maximum time allowed.

How do I fix heroku H12 error?

A restart cancels long-running requests. Restarting can resolve H12 issues because freshly-booted dynos receive requests without interference from long-running requests. Using the Heroku CLI, run heroku ps:restart web to restart all web dynos. or, using the Heroku Dashboard, click More, then Restart all dynos.


1 Answers

I found a solution :

EventListener

$tokenController = new FOS\OAuthServerBundle\Controller\TokenController($this->oauth2); //  $this->oauth2 is the @fos_oauth_server.server service
$credentials = $tokenController->tokenAction($request);
$response = $credentials->getContent();

This is just another solution to log in my API, whithout using cURL and the URL that give the token.

like image 87
Maxime Picard Avatar answered Sep 23 '22 15:09

Maxime Picard