Vkontakte.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Vkontakte extends AbstractService
  11. {
  12. /**
  13. * Defined scopes
  14. *
  15. * @link http://vk.com/dev/permissions
  16. */
  17. const SCOPE_EMAIL = 'email';
  18. const SCOPE_NOTIFY = 'notify';
  19. const SCOPE_FRIENDS = 'friends';
  20. const SCOPE_PHOTOS = 'photos';
  21. const SCOPE_AUDIO = 'audio';
  22. const SCOPE_VIDEO = 'video';
  23. const SCOPE_DOCS = 'docs';
  24. const SCOPE_NOTES = 'notes';
  25. const SCOPE_PAGES = 'pages';
  26. const SCOPE_APP_LINK = '';
  27. const SCOPE_STATUS = 'status';
  28. const SCOPE_OFFERS = 'offers';
  29. const SCOPE_QUESTIONS = 'questions';
  30. const SCOPE_WALL = 'wall';
  31. const SCOPE_GROUPS = 'groups';
  32. const SCOPE_MESSAGES = 'messages';
  33. const SCOPE_NOTIFICATIONS = 'notifications';
  34. const SCOPE_STATS = 'stats';
  35. const SCOPE_ADS = 'ads';
  36. const SCOPE_OFFLINE = 'offline';
  37. const SCOPE_NOHTTPS = 'nohttps';
  38. public function __construct(
  39. CredentialsInterface $credentials,
  40. ClientInterface $httpClient,
  41. TokenStorageInterface $storage,
  42. $scopes = array(),
  43. UriInterface $baseApiUri = null
  44. ) {
  45. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  46. if (null === $baseApiUri) {
  47. $this->baseApiUri = new Uri('https://api.vk.com/method/');
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getAuthorizationEndpoint()
  54. {
  55. return new Uri('https://oauth.vk.com/authorize');
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getAccessTokenEndpoint()
  61. {
  62. return new Uri('https://oauth.vk.com/access_token');
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function parseAccessTokenResponse($responseBody)
  68. {
  69. $data = json_decode($responseBody, true);
  70. if (null === $data || !is_array($data)) {
  71. throw new TokenResponseException('Unable to parse response.');
  72. } elseif (isset($data['error'])) {
  73. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  74. }
  75. $token = new StdOAuth2Token();
  76. $token->setAccessToken($data['access_token']);
  77. $token->setLifeTime($data['expires_in']);
  78. if (isset($data['refresh_token'])) {
  79. $token->setRefreshToken($data['refresh_token']);
  80. unset($data['refresh_token']);
  81. }
  82. unset($data['access_token']);
  83. unset($data['expires_in']);
  84. $token->setExtraParams($data);
  85. return $token;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. protected function getAuthorizationMethod()
  91. {
  92. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  93. }
  94. }