Pinterest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Pinterest service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link https://developers.pinterest.com/docs/api/overview/
  8. */
  9. namespace OAuth\OAuth2\Service;
  10. use OAuth\OAuth2\Token\StdOAuth2Token;
  11. use OAuth\Common\Http\Exception\TokenResponseException;
  12. use OAuth\Common\Http\Uri\Uri;
  13. use OAuth\Common\Consumer\CredentialsInterface;
  14. use OAuth\Common\Http\Client\ClientInterface;
  15. use OAuth\Common\Storage\TokenStorageInterface;
  16. use OAuth\Common\Http\Uri\UriInterface;
  17. /**
  18. * Pinterest service.
  19. *
  20. * @author Pedro Amorim <contact@pamorim.fr>
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link https://developers.pinterest.com/docs/api/overview/
  23. */
  24. class Pinterest extends AbstractService
  25. {
  26. /**
  27. * Defined scopes - More scopes are listed here:
  28. * https://developers.pinterest.com/docs/api/overview/
  29. */
  30. const SCOPE_READ_PUBLIC = 'read_public'; // read a user’s Pins, boards and likes
  31. const SCOPE_WRITE_PUBLIC = 'write_public'; // write Pins, boards, likes
  32. const SCOPE_READ_RELATIONSHIPS = 'read_relationships'; // read a user’s follows (boards, users, interests)
  33. const SCOPE_WRITE_RELATIONSHIPS = 'write_relationships'; // follow boards, users and interests
  34. public function __construct(
  35. CredentialsInterface $credentials,
  36. ClientInterface $httpClient,
  37. TokenStorageInterface $storage,
  38. $scopes = array(),
  39. UriInterface $baseApiUri = null
  40. ) {
  41. parent::__construct(
  42. $credentials,
  43. $httpClient,
  44. $storage,
  45. $scopes,
  46. $baseApiUri,
  47. true
  48. );
  49. if (null === $baseApiUri) {
  50. $this->baseApiUri = new Uri('https://api.pinterest.com/');
  51. }
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getAuthorizationEndpoint()
  57. {
  58. return new Uri('https://api.pinterest.com/oauth/');
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getAccessTokenEndpoint()
  64. {
  65. return new Uri('https://api.pinterest.com/v1/oauth/token');
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function getAuthorizationMethod()
  71. {
  72. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function parseAccessTokenResponse($responseBody)
  78. {
  79. $data = json_decode($responseBody, true);
  80. if (null === $data || !is_array($data)) {
  81. throw new TokenResponseException('Unable to parse response.');
  82. } elseif (isset($data['error'])) {
  83. throw new TokenResponseException(
  84. 'Error in retrieving token: "' . $data['error'] . '"'
  85. );
  86. }
  87. $token = new StdOAuth2Token();
  88. $token->setAccessToken($data['access_token']);
  89. if (isset($data['expires_in'])) {
  90. $token->setLifeTime($data['expires_in']);
  91. unset($data['expires_in']);
  92. }
  93. // I hope one day Pinterest add a refresh token :)
  94. if (isset($data['refresh_token'])) {
  95. $token->setRefreshToken($data['refresh_token']);
  96. unset($data['refresh_token']);
  97. }
  98. unset($data['access_token']);
  99. $token->setExtraParams($data);
  100. return $token;
  101. }
  102. }