/**
  * @expectedException \Happyr\LinkedIn\Exception\LinkedInTransferException
  */
 public function testConvertToSimpleXmlError()
 {
     $body = '{Foo: bar}';
     $response = new Response(200, [], $body);
     $result = ResponseConverter::convertToSimpleXml($response);
     $this->assertInstanceOf('\\SimpleXMLElement', $result);
     $this->assertEquals('foo', $result->firstname);
 }
 /**
  * {@inheritdoc}
  */
 public function api($method, $resource, array $options = array())
 {
     // Add access token to the headers
     $options['headers']['Authorization'] = sprintf('Bearer %s', (string) $this->getAccessToken());
     // Do logic and adjustments to the options
     $requestFormat = $this->filterRequestOption($options);
     // Generate an url
     $url = $this->getUrlGenerator()->getUrl('api', $resource, isset($options['query']) ? $options['query'] : array());
     $body = isset($options['body']) ? $options['body'] : null;
     $this->lastResponse = $this->getRequestManager()->sendRequest($method, $url, $options['headers'], $body);
     //Get the response data format
     if (isset($options['response_data_type'])) {
         $responseDataType = $options['response_data_type'];
     } else {
         $responseDataType = $this->getResponseDataType();
     }
     return ResponseConverter::convert($this->lastResponse, $requestFormat, $responseDataType);
 }
 /**
  * Retrieves an access token for the given authorization code
  * (previously generated from www.linkedin.com on behalf of
  * a specific user). The authorization code is sent to www.linkedin.com
  * and a legitimate access token is generated provided the access token
  * and the user for which it was generated all match, and the user is
  * either logged in to LinkedIn or has granted an offline access permission.
  *
  * @param LinkedInUrlGeneratorInterface $urlGenerator
  * @param string                        $code         An authorization code.
  *
  * @return AccessToken An access token exchanged for the authorization code.
  *
  * @throws LinkedInException
  */
 protected function getAccessTokenFromCode(LinkedInUrlGeneratorInterface $urlGenerator, $code)
 {
     if (empty($code)) {
         throw new LinkedInException('Could not get access token: The code was empty.');
     }
     $redirectUri = $this->getStorage()->get('redirect_uri');
     try {
         $url = $urlGenerator->getUrl('www', 'oauth/v2/accessToken');
         $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
         $body = http_build_query(['grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $redirectUri, 'client_id' => $this->appId, 'client_secret' => $this->appSecret]);
         $response = ResponseConverter::convertToArray($this->getRequestManager()->sendRequest('POST', $url, $headers, $body));
     } catch (LinkedInTransferException $e) {
         // most likely that user very recently revoked authorization.
         // In any event, we don't have an access token, so throw an exception.
         throw new LinkedInException('Could not get access token: The user may have revoked the authorization response from LinkedIn.com was empty.', $e->getCode(), $e);
     }
     if (empty($response)) {
         throw new LinkedInException('Could not get access token: The response from LinkedIn.com was empty.');
     }
     $tokenData = array_merge(['access_token' => null, 'expires_in' => null], $response);
     $token = new AccessToken($tokenData['access_token'], $tokenData['expires_in']);
     if (!$token->hasToken()) {
         throw new LinkedInException('Could not get access token: The response from LinkedIn.com did not contain a token.');
     }
     return $token;
 }