Xing.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace OAuth\OAuth1\Service;
  3. use OAuth\OAuth1\Signature\SignatureInterface;
  4. use OAuth\OAuth1\Token\StdOAuth1Token;
  5. use OAuth\Common\Http\Exception\TokenResponseException;
  6. use OAuth\Common\Http\Uri\Uri;
  7. use OAuth\Common\Consumer\CredentialsInterface;
  8. use OAuth\Common\Http\Uri\UriInterface;
  9. use OAuth\Common\Storage\TokenStorageInterface;
  10. use OAuth\Common\Http\Client\ClientInterface;
  11. class Xing extends AbstractService
  12. {
  13. public function __construct(
  14. CredentialsInterface $credentials,
  15. ClientInterface $httpClient,
  16. TokenStorageInterface $storage,
  17. SignatureInterface $signature,
  18. UriInterface $baseApiUri = null
  19. ) {
  20. parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
  21. if (null === $baseApiUri) {
  22. $this->baseApiUri = new Uri('https://api.xing.com/v1/');
  23. }
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getAuthorizationEndpoint()
  29. {
  30. return new Uri('https://api.xing.com/v1/authorize');
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getAccessTokenEndpoint()
  36. {
  37. return new Uri('https://api.xing.com/v1/access_token');
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getRequestTokenEndpoint()
  43. {
  44. return new Uri('https://api.xing.com/v1/request_token');
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function parseRequestTokenResponse($responseBody)
  50. {
  51. parse_str($responseBody, $data);
  52. if (null === $data || !is_array($data)) {
  53. throw new TokenResponseException('Unable to parse response.');
  54. } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
  55. throw new TokenResponseException('Error in retrieving token.');
  56. }
  57. return $this->parseAccessTokenResponse($responseBody);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function parseAccessTokenResponse($responseBody)
  63. {
  64. parse_str($responseBody, $data);
  65. $errors = json_decode($responseBody);
  66. if (null === $data || !is_array($data)) {
  67. throw new TokenResponseException('Unable to parse response.');
  68. } elseif ($errors) {
  69. throw new TokenResponseException('Error in retrieving token: "' . $errors->error_name . '"');
  70. }
  71. $token = new StdOAuth1Token();
  72. $token->setRequestToken($data['oauth_token']);
  73. $token->setRequestTokenSecret($data['oauth_token_secret']);
  74. $token->setAccessToken($data['oauth_token']);
  75. $token->setAccessTokenSecret($data['oauth_token_secret']);
  76. $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
  77. unset($data['oauth_token'], $data['oauth_token_secret']);
  78. $token->setExtraParams($data);
  79. return $token;
  80. }
  81. }