Yahoo.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. class Yahoo extends AbstractService
  7. {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function getAuthorizationEndpoint()
  12. {
  13. return new Uri('https://api.login.yahoo.com/oauth2/request_auth');
  14. }
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function getAccessTokenEndpoint()
  19. {
  20. return new Uri('https://api.login.yahoo.com/oauth2/get_token');
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function getAuthorizationMethod()
  26. {
  27. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function parseAccessTokenResponse($responseBody)
  33. {
  34. $data = json_decode($responseBody, true);
  35. if (null === $data || !is_array($data))
  36. {
  37. throw new TokenResponseException('Unable to parse response.');
  38. } elseif (isset($data['error']))
  39. {
  40. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  41. }
  42. $token = new StdOAuth2Token();
  43. $token->setAccessToken($data['access_token']);
  44. $token->setLifetime($data['expires_in']);
  45. if (isset($data['refresh_token']))
  46. {
  47. $token->setRefreshToken($data['refresh_token']);
  48. unset($data['refresh_token']);
  49. }
  50. unset($data['access_token']);
  51. unset($data['expires_in']);
  52. $token->setExtraParams($data);
  53. return $token;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function getExtraOAuthHeaders()
  59. {
  60. $encodedCredentials = base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret());
  61. return array('Authorization' => 'Basic ' . $encodedCredentials);
  62. }
  63. }