Yahoo.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\OAuth1\Token\TokenInterface;
  12. class Yahoo extends AbstractService
  13. {
  14. public function __construct(
  15. CredentialsInterface $credentials,
  16. ClientInterface $httpClient,
  17. TokenStorageInterface $storage,
  18. SignatureInterface $signature,
  19. UriInterface $baseApiUri = null
  20. ) {
  21. parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
  22. if (null === $baseApiUri) {
  23. $this->baseApiUri = new Uri('https://social.yahooapis.com/v1/');
  24. }
  25. }
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function getRequestTokenEndpoint()
  30. {
  31. return new Uri('https://api.login.yahoo.com/oauth/v2/get_request_token');
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getAuthorizationEndpoint()
  37. {
  38. return new Uri('https://api.login.yahoo.com/oauth/v2/request_auth');
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getAccessTokenEndpoint()
  44. {
  45. return new Uri('https://api.login.yahoo.com/oauth/v2/get_token');
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function refreshAccessToken(TokenInterface $token)
  51. {
  52. $extraParams = $token->getExtraParams();
  53. $bodyParams = array('oauth_session_handle' => $extraParams['oauth_session_handle']);
  54. $authorizationHeader = array(
  55. 'Authorization' => $this->buildAuthorizationHeaderForAPIRequest(
  56. 'POST',
  57. $this->getAccessTokenEndpoint(),
  58. $this->storage->retrieveAccessToken($this->service()),
  59. $bodyParams
  60. )
  61. );
  62. $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders(), array());
  63. $responseBody = $this->httpClient->retrieveResponse($this->getAccessTokenEndpoint(), $bodyParams, $headers);
  64. $token = $this->parseAccessTokenResponse($responseBody);
  65. $this->storage->storeAccessToken($this->service(), $token);
  66. return $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.');
  89. } elseif (isset($data['error'])) {
  90. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  91. }
  92. $token = new StdOAuth1Token();
  93. $token->setRequestToken($data['oauth_token']);
  94. $token->setRequestTokenSecret($data['oauth_token_secret']);
  95. $token->setAccessToken($data['oauth_token']);
  96. $token->setAccessTokenSecret($data['oauth_token_secret']);
  97. if (isset($data['oauth_expires_in'])) {
  98. $token->setLifetime($data['oauth_expires_in']);
  99. } else {
  100. $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
  101. }
  102. unset($data['oauth_token'], $data['oauth_token_secret']);
  103. $token->setExtraParams($data);
  104. return $token;
  105. }
  106. }