OAuthOAuth2ServiceMailchimp

The Gravity Forms Salesforce Add-On OAuth OAuth2 Service Mailchimp class.

Defined (1)

The class is defined in the following location(s).

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