Google.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\Common\Consumer\CredentialsInterface;
  4. use OAuth\Common\Http\Client\ClientInterface;
  5. use OAuth\Common\Http\Uri\UriInterface;
  6. use OAuth\Common\Storage\TokenStorageInterface;
  7. use OAuth\OAuth2\Token\StdOAuth2Token;
  8. use OAuth\Common\Http\Exception\TokenResponseException;
  9. use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
  10. use OAuth\Common\Http\Uri\Uri;
  11. class Google extends AbstractService
  12. {
  13. /**
  14. * Defined scopes - More scopes are listed here:
  15. * https://developers.google.com/oauthplayground/
  16. *
  17. * Make a pull request if you need more scopes.
  18. */
  19. // Basic
  20. const SCOPE_EMAIL = 'email';
  21. const SCOPE_PROFILE = 'profile';
  22. const SCOPE_USERINFO_EMAIL = 'https://www.googleapis.com/auth/userinfo.email';
  23. const SCOPE_USERINFO_PROFILE = 'https://www.googleapis.com/auth/userinfo.profile';
  24. // Google+
  25. const SCOPE_GPLUS_ME = 'https://www.googleapis.com/auth/plus.me';
  26. const SCOPE_GPLUS_LOGIN = 'https://www.googleapis.com/auth/plus.login';
  27. const SCOPE_GPLUS_CIRCLES_READ = 'https://www.googleapis.com/auth/plus.circles.read';
  28. const SCOPE_GPLUS_CIRCLES_WRITE = 'https://www.googleapis.com/auth/plus.circles.write';
  29. const SCOPE_GPLUS_STREAM_READ = 'https://www.googleapis.com/auth/plus.stream.read';
  30. const SCOPE_GPLUS_STREAM_WRITE = 'https://www.googleapis.com/auth/plus.stream.write';
  31. const SCOPE_GPLUS_MEDIA = 'https://www.googleapis.com/auth/plus.media.upload';
  32. // Google Drive
  33. const SCOPE_DOCUMENTSLIST = 'https://docs.google.com/feeds/';
  34. const SCOPE_SPREADSHEETS = 'https://spreadsheets.google.com/feeds/';
  35. const SCOPE_GOOGLEDRIVE = 'https://www.googleapis.com/auth/drive';
  36. const SCOPE_DRIVE_APPS = 'https://www.googleapis.com/auth/drive.appdata';
  37. const SCOPE_DRIVE_APPS_READ_ONLY = 'https://www.googleapis.com/auth/drive.apps.readonly';
  38. const SCOPE_GOOGLEDRIVE_FILES = 'https://www.googleapis.com/auth/drive.file';
  39. const SCOPE_DRIVE_METADATA_READ_ONLY = 'https://www.googleapis.com/auth/drive.metadata.readonly';
  40. const SCOPE_DRIVE_READ_ONLY = 'https://www.googleapis.com/auth/drive.readonly';
  41. const SCOPE_DRIVE_SCRIPTS = 'https://www.googleapis.com/auth/drive.scripts';
  42. // Adwords
  43. const SCOPE_ADSENSE = 'https://www.googleapis.com/auth/adsense';
  44. const SCOPE_ADWORDS = 'https://www.googleapis.com/auth/adwords/';
  45. const SCOPE_GAN = 'https://www.googleapis.com/auth/gan'; // google affiliate network...?
  46. //Doubleclick for Publishers
  47. const SCOPE_DFP = 'https://www.googleapis.com/auth/dfp';
  48. const SCOPE_DFP_TRAFFICKING = 'https://www.googleapis.com/auth/dfatrafficking';
  49. const SCOPE_DFP_REPORTING = 'https://www.googleapis.com/auth/dfareporting';
  50. // Google Analytics
  51. const SCOPE_ANALYTICS = 'https://www.googleapis.com/auth/analytics';
  52. const SCOPE_ANALYTICS_EDIT = 'https://www.googleapis.com/auth/analytics.edit';
  53. const SCOPE_ANALYTICS_MANAGE_USERS = 'https://www.googleapis.com/auth/analytics.manage.users';
  54. const SCOPE_ANALYTICS_READ_ONLY = 'https://www.googleapis.com/auth/analytics.readonly';
  55. //Gmail
  56. const SCOPE_GMAIL_MODIFY = 'https://www.googleapis.com/auth/gmail.modify';
  57. const SCOPE_GMAIL_READONLY = 'https://www.googleapis.com/auth/gmail.readonly';
  58. const SCOPE_GMAIL_COMPOSE = 'https://www.googleapis.com/auth/gmail.compose';
  59. const SCOPE_GMAIL_SEND = 'https://www.googleapis.com/auth/gmail.send';
  60. const SCOPE_GMAIL_INSERT = 'https://www.googleapis.com/auth/gmail.insert';
  61. const SCOPE_GMAIL_LABELS = 'https://www.googleapis.com/auth/gmail.labels';
  62. const SCOPE_GMAIL_FULL = 'https://mail.google.com/';
  63. // Other services
  64. const SCOPE_BOOKS = 'https://www.googleapis.com/auth/books';
  65. const SCOPE_BLOGGER = 'https://www.googleapis.com/auth/blogger';
  66. const SCOPE_CALENDAR = 'https://www.googleapis.com/auth/calendar';
  67. const SCOPE_CALENDAR_READ_ONLY = 'https://www.googleapis.com/auth/calendar.readonly';
  68. const SCOPE_CONTACT = 'https://www.google.com/m8/feeds/';
  69. const SCOPE_CONTACTS_RO = 'https://www.googleapis.com/auth/contacts.readonly';
  70. const SCOPE_CHROMEWEBSTORE = 'https://www.googleapis.com/auth/chromewebstore.readonly';
  71. const SCOPE_GMAIL = 'https://mail.google.com/mail/feed/atom';
  72. const SCOPE_GMAIL_IMAP_SMTP = 'https://mail.google.com';
  73. const SCOPE_PICASAWEB = 'https://picasaweb.google.com/data/';
  74. const SCOPE_SITES = 'https://sites.google.com/feeds/';
  75. const SCOPE_URLSHORTENER = 'https://www.googleapis.com/auth/urlshortener';
  76. const SCOPE_WEBMASTERTOOLS = 'https://www.google.com/webmasters/tools/feeds/';
  77. const SCOPE_TASKS = 'https://www.googleapis.com/auth/tasks';
  78. // Cloud services
  79. const SCOPE_CLOUDSTORAGE = 'https://www.googleapis.com/auth/devstorage.read_write';
  80. const SCOPE_CONTENTFORSHOPPING = 'https://www.googleapis.com/auth/structuredcontent'; // what even is this
  81. const SCOPE_USER_PROVISIONING = 'https://apps-apis.google.com/a/feeds/user/';
  82. const SCOPE_GROUPS_PROVISIONING = 'https://apps-apis.google.com/a/feeds/groups/';
  83. const SCOPE_NICKNAME_PROVISIONING = 'https://apps-apis.google.com/a/feeds/alias/';
  84. // Old
  85. const SCOPE_ORKUT = 'https://www.googleapis.com/auth/orkut';
  86. const SCOPE_GOOGLELATITUDE =
  87. 'https://www.googleapis.com/auth/latitude.all.best https://www.googleapis.com/auth/latitude.all.city';
  88. const SCOPE_OPENID = 'openid';
  89. // YouTube
  90. const SCOPE_YOUTUBE_GDATA = 'https://gdata.youtube.com';
  91. const SCOPE_YOUTUBE_ANALYTICS_MONETARY = 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly';
  92. const SCOPE_YOUTUBE_ANALYTICS = 'https://www.googleapis.com/auth/yt-analytics.readonly';
  93. const SCOPE_YOUTUBE = 'https://www.googleapis.com/auth/youtube';
  94. const SCOPE_YOUTUBE_READ_ONLY = 'https://www.googleapis.com/auth/youtube.readonly';
  95. const SCOPE_YOUTUBE_UPLOAD = 'https://www.googleapis.com/auth/youtube.upload';
  96. const SCOPE_YOUTUBE_PARTNER = 'https://www.googleapis.com/auth/youtubepartner';
  97. const SCOPE_YOUTUBE_PARTNER_AUDIT = 'https://www.googleapis.com/auth/youtubepartner-channel-audit';
  98. // Google Glass
  99. const SCOPE_GLASS_TIMELINE = 'https://www.googleapis.com/auth/glass.timeline';
  100. const SCOPE_GLASS_LOCATION = 'https://www.googleapis.com/auth/glass.location';
  101. // Android Publisher
  102. const SCOPE_ANDROID_PUBLISHER = 'https://www.googleapis.com/auth/androidpublisher';
  103. // Google Classroom
  104. const SCOPE_CLASSROOM_COURSES = 'https://www.googleapis.com/auth/classroom.courses';
  105. const SCOPE_CLASSROOM_COURSES_READONLY = 'https://www.googleapis.com/auth/classroom.courses.readonly';
  106. const SCOPE_CLASSROOM_PROFILE_EMAILS = 'https://www.googleapis.com/auth/classroom.profile.emails';
  107. const SCOPE_CLASSROOM_PROFILE_PHOTOS = 'https://www.googleapis.com/auth/classroom.profile.photos';
  108. const SCOPE_CLASSROOM_ROSTERS = 'https://www.googleapis.com/auth/classroom.rosters';
  109. const SCOPE_CLASSROOM_ROSTERS_READONLY = 'https://www.googleapis.com/auth/classroom.rosters.readonly';
  110. protected $accessType = 'online';
  111. public function __construct(
  112. CredentialsInterface $credentials,
  113. ClientInterface $httpClient,
  114. TokenStorageInterface $storage,
  115. $scopes = array(),
  116. UriInterface $baseApiUri = null
  117. ) {
  118. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  119. if (null === $baseApiUri) {
  120. $this->baseApiUri = new Uri('https://www.googleapis.com/oauth2/v1/');
  121. }
  122. }
  123. public function setAccessType($accessType)
  124. {
  125. if (!in_array($accessType, array('online', 'offline'), true)) {
  126. throw new InvalidAccessTypeException('Invalid accessType, expected either online or offline');
  127. }
  128. $this->accessType = $accessType;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getAuthorizationEndpoint()
  134. {
  135. return new Uri('https://accounts.google.com/o/oauth2/auth?access_type=' . $this->accessType);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getAccessTokenEndpoint()
  141. {
  142. return new Uri('https://accounts.google.com/o/oauth2/token');
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. protected function parseAccessTokenResponse($responseBody)
  148. {
  149. $data = json_decode($responseBody, true);
  150. if (null === $data || !is_array($data)) {
  151. throw new TokenResponseException('Unable to parse response.');
  152. } elseif (isset($data['error'])) {
  153. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  154. }
  155. $token = new StdOAuth2Token();
  156. $token->setAccessToken($data['access_token']);
  157. $token->setLifetime($data['expires_in']);
  158. if (isset($data['refresh_token'])) {
  159. $token->setRefreshToken($data['refresh_token']);
  160. unset($data['refresh_token']);
  161. }
  162. unset($data['access_token']);
  163. unset($data['expires_in']);
  164. $token->setExtraParams($data);
  165. return $token;
  166. }
  167. }