Mailchimp.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. use OAuth\Common\Consumer\CredentialsInterface;
  7. use OAuth\Common\Http\Client\ClientInterface;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. use OAuth\Common\Http\Uri\UriInterface;
  10. class Mailchimp extends AbstractService
  11. {
  12. public function __construct(
  13. CredentialsInterface $credentials,
  14. ClientInterface $httpClient,
  15. TokenStorageInterface $storage,
  16. $scopes = array(),
  17. UriInterface $baseApiUri = null
  18. ) {
  19. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  20. if (is_null($this->baseApiUri) && $storage->hasAccessToken($this->service())) {
  21. $this->setBaseApiUri($storage->retrieveAccessToken($this->service()));
  22. }
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getAuthorizationMethod()
  28. {
  29. return static::AUTHORIZATION_METHOD_QUERY_STRING_V3;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getAuthorizationEndpoint()
  35. {
  36. return new Uri('https://login.mailchimp.com/oauth2/authorize');
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getAccessTokenEndpoint()
  42. {
  43. return new Uri('https://login.mailchimp.com/oauth2/token');
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function parseAccessTokenResponse($responseBody)
  49. {
  50. // Parse JSON
  51. $data = json_decode($responseBody, true);
  52. // Do validation.
  53. if (null === $data || !is_array($data)) {
  54. throw new TokenResponseException('Unable to parse response.');
  55. } elseif (isset($data['error'])) {
  56. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  57. }
  58. // Create token object.
  59. $token = new StdOAuth2Token($data['access_token']);
  60. // Set the right API endpoint.
  61. $this->setBaseApiUri($token);
  62. // Mailchimp tokens evidently never expire...
  63. $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
  64. return $token;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
  70. {
  71. if (is_null($this->baseApiUri)) {
  72. $this->setBaseApiUri($this->storage->retrieveAccessToken($this->service()));
  73. }
  74. return parent::request($path, $method, $body, $extraHeaders);
  75. }
  76. /**
  77. * Set the right base endpoint.
  78. *
  79. * @param StdOAuth2Token $token
  80. */
  81. protected function setBaseApiUri(StdOAuth2Token $token)
  82. {
  83. // Make request uri.
  84. $endpoint = 'https://login.mailchimp.com/oauth2/metadata?oauth_token='. $token->getAccessToken();
  85. // Grab meta data about the token.
  86. $response = $this->httpClient->retrieveResponse(new Uri($endpoint), array(), array(), 'GET');
  87. // Parse JSON.
  88. $meta = json_decode($response, true);
  89. // Set base api uri.
  90. $this->baseApiUri = new Uri('https://'. $meta['dc'] .'.api.mailchimp.com/2.0/');
  91. // Allow chaining.
  92. return $this;
  93. }
  94. }