g_letter.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Drive API Quickstart</title>
  5. <meta charset="utf-8" />
  6. </head>
  7. <body>
  8. <p>Drive API Quickstart</p>
  9. <!--Add buttons to initiate auth sequence and sign out-->
  10. <button id="authorize_button" onclick="handleAuthClick()">Authorize</button>
  11. <button id="signout_button" onclick="handleSignoutClick()">Sign Out</button>
  12. <pre id="content" style="white-space: pre-wrap;"></pre>
  13. <script type="text/javascript">
  14. /* exported gapiLoaded */
  15. /* exported gisLoaded */
  16. /* exported handleAuthClick */
  17. /* exported handleSignoutClick */
  18. // TODO(developer): Set to client ID and API key from the Developer Console
  19. const CLIENT_ID = '165389691568-f1mia3sfncb6tb7d18lm49nbr0qkkf4v.apps.googleusercontent.com';
  20. const API_KEY = 'AIzaSyC0FbRt_34qIq_FW2UXxTbYftwIg9FsiW4';
  21. // Discovery doc URL for APIs used by the quickstart
  22. const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest';
  23. // Authorization scopes required by the API; multiple scopes can be
  24. // included, separated by spaces.
  25. const SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly';
  26. let tokenClient;
  27. let gapiInited = false;
  28. let gisInited = false;
  29. document.getElementById('authorize_button').style.visibility = 'hidden';
  30. document.getElementById('signout_button').style.visibility = 'hidden';
  31. /**
  32. * Callback after api.js is loaded.
  33. */
  34. function gapiLoaded() {
  35. gapi.load('client', initializeGapiClient);
  36. }
  37. /**
  38. * Callback after the API client is loaded. Loads the
  39. * discovery doc to initialize the API.
  40. */
  41. async function initializeGapiClient() {
  42. await gapi.client.init({
  43. apiKey: API_KEY,
  44. discoveryDocs: [DISCOVERY_DOC],
  45. });
  46. gapiInited = true;
  47. maybeEnableButtons();
  48. }
  49. /**
  50. * Callback after Google Identity Services are loaded.
  51. */
  52. function gisLoaded() {
  53. tokenClient = google.accounts.oauth2.initTokenClient({
  54. client_id: CLIENT_ID,
  55. scope: SCOPES,
  56. callback: '', // defined later
  57. });
  58. gisInited = true;
  59. maybeEnableButtons();
  60. }
  61. /**
  62. * Enables user interaction after all libraries are loaded.
  63. */
  64. function maybeEnableButtons() {
  65. if (gapiInited && gisInited) {
  66. document.getElementById('authorize_button').style.visibility = 'visible';
  67. }
  68. }
  69. /**
  70. * Sign in the user upon button click.
  71. */
  72. function handleAuthClick() {
  73. tokenClient.callback = async (resp) => {
  74. if (resp.error !== undefined) {
  75. throw (resp);
  76. }
  77. document.getElementById('signout_button').style.visibility = 'visible';
  78. document.getElementById('authorize_button').innerText = 'Refresh';
  79. await listFiles();
  80. };
  81. if (gapi.client.getToken() === null) {
  82. // Prompt the user to select a Google Account and ask for consent to share their data
  83. // when establishing a new session.
  84. tokenClient.requestAccessToken({prompt: 'consent'});
  85. } else {
  86. // Skip display of account chooser and consent dialog for an existing session.
  87. tokenClient.requestAccessToken({prompt: ''});
  88. }
  89. }
  90. /**
  91. * Sign out the user upon button click.
  92. */
  93. function handleSignoutClick() {
  94. const token = gapi.client.getToken();
  95. if (token !== null) {
  96. google.accounts.oauth2.revoke(token.access_token);
  97. gapi.client.setToken('');
  98. document.getElementById('content').innerText = '';
  99. document.getElementById('authorize_button').innerText = 'Authorize';
  100. document.getElementById('signout_button').style.visibility = 'hidden';
  101. }
  102. }
  103. /**
  104. * Print metadata for first 10 files.
  105. */
  106. async function listFiles() {
  107. let response;
  108. try {
  109. response = await gapi.client.drive.files.list({
  110. 'pageSize': 10,
  111. 'fields': 'files(id, name)',
  112. });
  113. } catch (err) {
  114. document.getElementById('content').innerText = err.message;
  115. return;
  116. }
  117. const files = response.result.files;
  118. if (!files || files.length == 0) {
  119. document.getElementById('content').innerText = 'No files found.';
  120. return;
  121. }
  122. // Flatten to string to display
  123. const output = files.reduce(
  124. (str, file) => `${str}${file.name} (${file.id})\n`,
  125. 'Files:\n');
  126. document.getElementById('content').innerText = output;
  127. }
  128. </script>
  129. <script async defer src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script>
  130. <script async defer src="https://accounts.google.com/gsi/client" onload="gisLoaded()"></script>
  131. </body>
  132. </html>