Twitter.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. use OAuth\Common\Exception\Exception;
  12. class Twitter extends AbstractService
  13. {
  14. const ENDPOINT_AUTHENTICATE = "https://api.twitter.com/oauth/authenticate";
  15. const ENDPOINT_AUTHORIZE = "https://api.twitter.com/oauth/authorize";
  16. protected $authorizationEndpoint = self::ENDPOINT_AUTHENTICATE;
  17. public function __construct(
  18. CredentialsInterface $credentials,
  19. ClientInterface $httpClient,
  20. TokenStorageInterface $storage,
  21. SignatureInterface $signature,
  22. UriInterface $baseApiUri = null
  23. ) {
  24. parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
  25. if (null === $baseApiUri) {
  26. $this->baseApiUri = new Uri('https://api.twitter.com/1.1/');
  27. }
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getRequestTokenEndpoint()
  33. {
  34. return new Uri('https://api.twitter.com/oauth/request_token');
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getAuthorizationEndpoint()
  40. {
  41. if ($this->authorizationEndpoint != self::ENDPOINT_AUTHENTICATE
  42. && $this->authorizationEndpoint != self::ENDPOINT_AUTHORIZE) {
  43. $this->authorizationEndpoint = self::ENDPOINT_AUTHENTICATE;
  44. }
  45. return new Uri($this->authorizationEndpoint);
  46. }
  47. /**
  48. * @param string $authorizationEndpoint
  49. *
  50. * @throws Exception
  51. */
  52. public function setAuthorizationEndpoint($endpoint)
  53. {
  54. if ($endpoint != self::ENDPOINT_AUTHENTICATE && $endpoint != self::ENDPOINT_AUTHORIZE) {
  55. throw new Exception(
  56. sprintf("'%s' is not a correct Twitter authorization endpoint.", $endpoint)
  57. );
  58. }
  59. $this->authorizationEndpoint = $endpoint;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getAccessTokenEndpoint()
  65. {
  66. return new Uri('https://api.twitter.com/oauth/access_token');
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function parseRequestTokenResponse($responseBody)
  72. {
  73. parse_str($responseBody, $data);
  74. if (null === $data || !is_array($data)) {
  75. throw new TokenResponseException('Unable to parse response.');
  76. } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
  77. throw new TokenResponseException('Error in retrieving token.');
  78. }
  79. return $this->parseAccessTokenResponse($responseBody);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function parseAccessTokenResponse($responseBody)
  85. {
  86. parse_str($responseBody, $data);
  87. if (null === $data || !is_array($data)) {
  88. throw new TokenResponseException('Unable to parse response: ' . $responseBody);
  89. } elseif (isset($data['error'])) {
  90. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  91. } elseif (!isset($data["oauth_token"]) || !isset($data["oauth_token_secret"])) {
  92. throw new TokenResponseException('Invalid response. OAuth Token data not set: ' . $responseBody);
  93. }
  94. $token = new StdOAuth1Token();
  95. $token->setRequestToken($data['oauth_token']);
  96. $token->setRequestTokenSecret($data['oauth_token_secret']);
  97. $token->setAccessToken($data['oauth_token']);
  98. $token->setAccessTokenSecret($data['oauth_token_secret']);
  99. $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
  100. unset($data['oauth_token'], $data['oauth_token_secret']);
  101. $token->setExtraParams($data);
  102. return $token;
  103. }
  104. }