Google.php 10 KB

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