OAuthOAuth2ServiceBitly

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

Defined (1)

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

/lib/PHPoAuthLib/src/OAuth/OAuth2/Service/Bitly.php  
  1. class Bitly extends AbstractService 
  2. public function __construct( 
  3. CredentialsInterface $credentials,  
  4. ClientInterface $httpClient,  
  5. TokenStorageInterface $storage,  
  6. $scopes = array(),  
  7. UriInterface $baseApiUri = null 
  8. ) { 
  9. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri); 
  10.  
  11. if (null === $baseApiUri) { 
  12. $this->baseApiUri = new Uri('https://api-ssl.bitly.com/v3/'); 
  13.  
  14. /** 
  15. * {@inheritdoc} 
  16. */ 
  17. public function getAuthorizationEndpoint() 
  18. return new Uri('https://bitly.com/oauth/authorize'); 
  19.  
  20. /** 
  21. * {@inheritdoc} 
  22. */ 
  23. public function getAccessTokenEndpoint() 
  24. return new Uri('https://api-ssl.bitly.com/oauth/access_token'); 
  25.  
  26. /** 
  27. * {@inheritdoc} 
  28. */ 
  29. protected function getAuthorizationMethod() 
  30. return static::AUTHORIZATION_METHOD_QUERY_STRING; 
  31.  
  32. /** 
  33. * {@inheritdoc} 
  34. */ 
  35. protected function parseAccessTokenResponse($responseBody) 
  36. $data = json_decode($responseBody, true); 
  37.  
  38. if (null === $data || !is_array($data)) { 
  39. throw new TokenResponseException('Unable to parse response.'); 
  40. } elseif (isset($data['error'])) { 
  41. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); 
  42.  
  43. $token = new StdOAuth2Token(); 
  44. $token->setAccessToken($data['access_token']); 
  45. // I'm invincible!!! 
  46. $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); 
  47. unset($data['access_token']); 
  48.  
  49. $token->setExtraParams($data); 
  50.  
  51. return $token; 
  52.  
  53. /** 
  54. * {@inheritdoc} 
  55. */ 
  56. public function requestAccessToken($code, $state = null) 
  57. if (null !== $state) { 
  58. $this->validateAuthorizationState($state); 
  59.  
  60. $bodyParams = array( 
  61. 'code' => $code,  
  62. 'client_id' => $this->credentials->getConsumerId(),  
  63. 'client_secret' => $this->credentials->getConsumerSecret(),  
  64. 'redirect_uri' => $this->credentials->getCallbackUrl(),  
  65. 'grant_type' => 'authorization_code',  
  66. ); 
  67.  
  68. $responseBody = $this->httpClient->retrieveResponse( 
  69. $this->getAccessTokenEndpoint(),  
  70. $bodyParams,  
  71. $this->getExtraOAuthHeaders() 
  72. ); 
  73.  
  74. // we can scream what we want that we want bitly to return a json encoded string (format=json), but the 
  75. // WOAH WATCH YOUR LANGUAGE ;) service doesn't seem to like screaming, hence we need to manually 
  76. // parse the result 
  77. $parsedResult = array(); 
  78. parse_str($responseBody, $parsedResult); 
  79.  
  80. $token = $this->parseAccessTokenResponse(json_encode($parsedResult)); 
  81. $this->storage->storeAccessToken($this->service(), $token); 
  82.  
  83. return $token;