private function retrieveAccessToken(PhabricatorOAuthProvider $provider)
 {
     $request = $this->getRequest();
     $token = $request->getStr('token');
     if ($token) {
         $this->tokenExpires = $request->getInt('expires');
         $this->accessToken = $token;
         $this->oauthState = $request->getStr('state');
         return null;
     }
     $client_id = $provider->getClientID();
     $client_secret = $provider->getClientSecret();
     $redirect_uri = $provider->getRedirectURI();
     $auth_uri = $provider->getTokenURI();
     $code = $request->getStr('code');
     $query_data = array('client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'code' => $code) + $provider->getExtraTokenParameters();
     $post_data = http_build_query($query_data);
     $post_length = strlen($post_data);
     $stream_context = stream_context_create(array('http' => array('method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n" . "Content-Length: {$post_length}\r\n", 'content' => $post_data)));
     $stream = fopen($auth_uri, 'r', false, $stream_context);
     $response = false;
     $meta = null;
     if ($stream) {
         $meta = stream_get_meta_data($stream);
         $response = stream_get_contents($stream);
         fclose($stream);
     }
     if ($response === false) {
         return $this->buildErrorResponse(new PhabricatorOAuthFailureView());
     }
     $data = $provider->decodeTokenResponse($response);
     $token = idx($data, 'access_token');
     if (!$token) {
         return $this->buildErrorResponse(new PhabricatorOAuthFailureView());
     }
     if (idx($data, 'expires')) {
         $this->tokenExpires = time() + $data['expires'];
     }
     $this->accessToken = $token;
     $this->oauthState = $request->getStr('state');
     return null;
 }
 private function retrieveAccessToken(PhabricatorOAuthProvider $provider)
 {
     $request = $this->getRequest();
     $token = $request->getStr('confirm_token');
     if ($token) {
         $this->tokenExpires = $request->getInt('expires');
         $this->accessToken = $token;
         $this->oauthState = $request->getStr('state');
         return null;
     }
     $client_id = $provider->getClientID();
     $client_secret = $provider->getClientSecret();
     $redirect_uri = $provider->getRedirectURI();
     $auth_uri = $provider->getTokenURI();
     $code = $request->getStr('code');
     $query_data = array('client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'code' => $code) + $provider->getExtraTokenParameters();
     $future = new HTTPSFuture($auth_uri, $query_data);
     $future->setMethod('POST');
     try {
         list($response) = $future->resolvex();
     } catch (Exception $ex) {
         return $this->buildErrorResponse(new PhabricatorOAuthFailureView());
     }
     $data = $provider->decodeTokenResponse($response);
     $token = idx($data, 'access_token');
     if (!$token) {
         return $this->buildErrorResponse(new PhabricatorOAuthFailureView());
     }
     $this->tokenExpires = $provider->getTokenExpiryFromArray($data);
     $this->accessToken = $token;
     $this->oauthState = $request->getStr('state');
     return null;
 }