Yammer.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Yammer 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 (null === $baseApiUri) {
  21. $this->baseApiUri = new Uri('https://www.yammer.com/api/v1/');
  22. }
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getAuthorizationEndpoint()
  28. {
  29. return new Uri('https://www.yammer.com/dialog/oauth');
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getAccessTokenEndpoint()
  35. {
  36. return new Uri('https://www.yammer.com/oauth2/access_token.json');
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getAuthorizationMethod()
  42. {
  43. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function parseAccessTokenResponse($responseBody)
  49. {
  50. $data = json_decode($responseBody, true);
  51. if (null === $data || !is_array($data)) {
  52. throw new TokenResponseException('Unable to parse response.');
  53. } elseif (isset($data['error'])) {
  54. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  55. }
  56. $token = new StdOAuth2Token();
  57. $token->setAccessToken($data['access_token']['token']);
  58. $token->setLifetime($data['access_token']['expires_at']);
  59. if (isset($data['refresh_token'])) {
  60. $token->setRefreshToken($data['refresh_token']);
  61. unset($data['refresh_token']);
  62. }
  63. unset($data['access_token']);
  64. unset($data['expires_in']);
  65. $token->setExtraParams($data);
  66. return $token;
  67. }
  68. }