You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

googleApi.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const GOOGLE_API_CLIENT_LIBRARY_URL = 'https://apis.google.com/js/api.js';
  2. const GOOGLE_API_SCOPES = [
  3. 'https://www.googleapis.com/auth/youtube.readonly'
  4. ].join(' ');
  5. /**
  6. * A promise for dynamically loading the Google API Client Library.
  7. *
  8. * @private
  9. * @type {Promise}
  10. */
  11. let googleClientLoadPromise;
  12. /**
  13. * A singleton for loading and interacting with the Google API.
  14. */
  15. const googleApi = {
  16. /**
  17. * Obtains Google API Client Library, loading the library dynamically if
  18. * needed.
  19. *
  20. * @returns {Promise}
  21. */
  22. get() {
  23. const globalGoogleApi = this._getGoogleApiClient();
  24. if (!globalGoogleApi) {
  25. return this.load();
  26. }
  27. return Promise.resolve(globalGoogleApi);
  28. },
  29. /**
  30. * Gets the profile for the user signed in to the Google API Client Library.
  31. *
  32. * @returns {Promise}
  33. */
  34. getCurrentUserProfile() {
  35. return this.get()
  36. .then(() => this.isSignedIn())
  37. .then(isSignedIn => {
  38. if (!isSignedIn) {
  39. return null;
  40. }
  41. return this._getGoogleApiClient()
  42. .auth2.getAuthInstance()
  43. .currentUser.get()
  44. .getBasicProfile();
  45. });
  46. },
  47. /**
  48. * Sets the Google Web Client ID used for authenticating with Google and
  49. * making Google API requests.
  50. *
  51. * @param {string} clientId - The client ID to be used with the API library.
  52. * @returns {Promise}
  53. */
  54. initializeClient(clientId) {
  55. return this.get()
  56. .then(api => new Promise((resolve, reject) => {
  57. // setTimeout is used as a workaround for api.client.init not
  58. // resolving consistently when the Google API Client Library is
  59. // loaded asynchronously. See:
  60. // github.com/google/google-api-javascript-client/issues/399
  61. setTimeout(() => {
  62. api.client.init({
  63. clientId,
  64. scope: GOOGLE_API_SCOPES
  65. })
  66. .then(resolve)
  67. .catch(reject);
  68. }, 500);
  69. }));
  70. },
  71. /**
  72. * Checks whether a user is currently authenticated with Google through an
  73. * initialized Google API Client Library.
  74. *
  75. * @returns {Promise}
  76. */
  77. isSignedIn() {
  78. return this.get()
  79. .then(api => Boolean(api
  80. && api.auth2
  81. && api.auth2.getAuthInstance
  82. && api.auth2.getAuthInstance().isSignedIn
  83. && api.auth2.getAuthInstance().isSignedIn.get()));
  84. },
  85. /**
  86. * Generates a script tag and downloads the Google API Client Library.
  87. *
  88. * @returns {Promise}
  89. */
  90. load() {
  91. if (googleClientLoadPromise) {
  92. return googleClientLoadPromise;
  93. }
  94. googleClientLoadPromise = new Promise((resolve, reject) => {
  95. const scriptTag = document.createElement('script');
  96. scriptTag.async = true;
  97. scriptTag.addEventListener('error', () => {
  98. scriptTag.remove();
  99. googleClientLoadPromise = null;
  100. reject();
  101. });
  102. scriptTag.addEventListener('load', resolve);
  103. scriptTag.type = 'text/javascript';
  104. scriptTag.src = GOOGLE_API_CLIENT_LIBRARY_URL;
  105. document.head.appendChild(scriptTag);
  106. })
  107. .then(() => new Promise((resolve, reject) =>
  108. this._getGoogleApiClient().load('client:auth2', {
  109. callback: resolve,
  110. onerror: reject
  111. })))
  112. .then(() => this._getGoogleApiClient());
  113. return googleClientLoadPromise;
  114. },
  115. /**
  116. * Executes a request for a list of all YouTube broadcasts associated with
  117. * user currently signed in to the Google API Client Library.
  118. *
  119. * @returns {Promise}
  120. */
  121. requestAvailableYouTubeBroadcasts() {
  122. const url = this._getURLForLiveBroadcasts();
  123. return this.get()
  124. .then(api => api.client.request(url));
  125. },
  126. /**
  127. * Executes a request to get all live streams associated with a broadcast
  128. * in YouTube.
  129. *
  130. * @param {string} boundStreamID - The bound stream ID associated with a
  131. * broadcast in YouTube.
  132. * @returns {Promise}
  133. */
  134. requestLiveStreamsForYouTubeBroadcast(boundStreamID) {
  135. const url = this._getURLForLiveStreams(boundStreamID);
  136. return this.get()
  137. .then(api => api.client.request(url));
  138. },
  139. /**
  140. * Prompts the participant to sign in to the Google API Client Library, even
  141. * if already signed in.
  142. *
  143. * @returns {Promise}
  144. */
  145. showAccountSelection() {
  146. return this.get()
  147. .then(api => api.auth2.getAuthInstance().signIn());
  148. },
  149. /**
  150. * Prompts the participant to sign in to the Google API Client Library, if
  151. * not already signed in.
  152. *
  153. * @returns {Promise}
  154. */
  155. signInIfNotSignedIn() {
  156. return this.get()
  157. .then(() => this.isSignedIn())
  158. .then(isSignedIn => {
  159. if (!isSignedIn) {
  160. return this.showAccountSelection();
  161. }
  162. });
  163. },
  164. /**
  165. * Returns the global Google API Client Library object. Direct use of this
  166. * method is discouraged; instead use the {@link get} method.
  167. *
  168. * @private
  169. * @returns {Object|undefined}
  170. */
  171. _getGoogleApiClient() {
  172. return window.gapi;
  173. },
  174. /**
  175. * Returns the URL to the Google API endpoint for retrieving the currently
  176. * signed in user's YouTube broadcasts.
  177. *
  178. * @private
  179. * @returns {string}
  180. */
  181. _getURLForLiveBroadcasts() {
  182. return [
  183. 'https://content.googleapis.com/youtube/v3/liveBroadcasts',
  184. '?broadcastType=persistent',
  185. '&mine=true&part=id%2Csnippet%2CcontentDetails%2Cstatus'
  186. ].join('');
  187. },
  188. /**
  189. * Returns the URL to the Google API endpoint for retrieving the live
  190. * streams associated with a YouTube broadcast's bound stream.
  191. *
  192. * @param {string} boundStreamID - The bound stream ID associated with a
  193. * broadcast in YouTube.
  194. * @returns {string}
  195. */
  196. _getURLForLiveStreams(boundStreamID) {
  197. return [
  198. 'https://content.googleapis.com/youtube/v3/liveStreams',
  199. '?part=id%2Csnippet%2Ccdn%2Cstatus',
  200. `&id=${boundStreamID}`
  201. ].join('');
  202. }
  203. };
  204. export default googleApi;