Web 应用的代码示例
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本指南介绍了如何使用 Google Picker API 功能,例如启用多选、隐藏导航窗格,以及选择具有应用当前 OAuth 2.0 令牌的用户账号。
前提条件
在此示例中,您需要指定以下几项:
同一 Google Cloud 项目必须同时包含客户端 ID 和应用 ID,因为它们用于授权访问用户的文件。
创建应用
以下代码示例展示了如何使用图片选择器或上传页面,用户可以从 Web 应用中的按钮打开该选择器或上传页面。
借助 setOAuthToken
函数,应用可以使用当前的身份验证令牌来确定 Google 选择器使用哪个 Google 账号来显示文件。如果用户登录了多个 Google 账号,Google 选择器可以显示相应授权账号的文件。
在打开文件时从 Google 选择器获取文件 ID 后,应用便可获取文件元数据并下载文件内容,如 files
资源的 get
方法中所述。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Code sample for web apps\n\nThis guide explains how to use Google Picker API features such as turning on\nmulti-select, hiding the navigation pane, and choosing the user account with the\napp's current OAuth 2.0 token.\n| **Note:** You can also use the [`@googleworkspace/drive-picker-element`](https://npmjs.com/package/@googleworkspace/drive-picker-element) web component to integrate the Google Picker into your web app.\n\nPrerequisites\n-------------\n\nFor this example, you need to specify several items:\n\n- To locate both the **Client ID** and the **API Key**:\n\n 1. In the Google Cloud console, go to Menu\n menu\n \\\u003e **APIs \\& Services**\n \\\u003e **Credentials**.\n\n [Go to Credentials](https://console.cloud.google.com/apis/credentials)\n- To locate the **App ID**:\n\n 1. In the Google Cloud console, go to Menu\n menu\n \\\u003e **IAM \\& Admin**\n \\\u003e **Settings**.\n\n [Go to Settings](https://console.cloud.google.com/iam-admin/settings)\n 2. Use the **project number** for the app ID.\n\nThe same Google Cloud project must contain both the client ID and the app ID as it's\nused to authorize access to a user's files.\n\nCreate the app\n--------------\n\nThe following code sample shows how to use an image selector or upload page that\nusers can open from a button in a web app. \ndrive/picker/helloworld.html \n[View on GitHub](https://github.com/googleworkspace/browser-samples/blob/main/drive/picker/helloworld.html) \n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n \u003ctitle\u003eGoogle Picker API Quickstart\u003c/title\u003e\n \u003cmeta charset=\"utf-8\" /\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\u003cp\u003eGoogle Picker API Quickstart\u003c/p\u003e\n\n\u003c!--Add buttons to initiate auth sequence and sign out.--\u003e\n\u003cbutton id=\"authorize_button\" onclick=\"handleAuthClick()\"\u003eAuthorize\u003c/button\u003e\n\u003cbutton id=\"signout_button\" onclick=\"handleSignoutClick()\"\u003eSign Out\u003c/button\u003e\n\n\u003cpre id=\"content\" style=\"white-space: pre-wrap;\"\u003e\u003c/pre\u003e\n\n\u003cscript type=\"text/javascript\"\u003e\n /* exported gapiLoaded */\n /* exported gisLoaded */\n /* exported handleAuthClick */\n /* exported handleSignoutClick */\n\n // Authorization scopes required by the API; multiple scopes can be\n // included, separated by spaces.\n const SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly';\n\n // TODO(developer): Replace with your client ID and API key from https://console.cloud.google.com/.\n const CLIENT_ID = '\u003cYOUR_CLIENT_ID\u003e';\n const API_KEY = '\u003cYOUR_API_KEY\u003e';\n\n // TODO(developer): Replace with your project number from https://console.cloud.google.com/.\n const APP_ID = '\u003cYOUR_APP_ID\u003e';\n\n let tokenClient;\n let accessToken = null;\n let pickerInited = false;\n let gisInited = false;\n\n document.getElementById('authorize_button').style.visibility = 'hidden';\n document.getElementById('signout_button').style.visibility = 'hidden';\n\n /**\n * Callback after api.js is loaded.\n */\n function gapiLoaded() {\n gapi.load('client:picker', initializePicker);\n }\n\n /**\n * Callback after the API client is loaded. Loads the\n * discovery doc to initialize the API.\n */\n async function initializePicker() {\n await gapi.client.load('https://www.googleapis.com/discovery/v1/apis/drive/v3/rest');\n pickerInited = true;\n maybeEnableButtons();\n }\n\n /**\n * Callback after Google Identity Services are loaded.\n */\n function gisLoaded() {\n tokenClient = google.accounts.oauth2.initTokenClient({\n client_id: CLIENT_ID,\n scope: SCOPES,\n callback: '', // defined later\n });\n gisInited = true;\n maybeEnableButtons();\n }\n\n /**\n * Enables user interaction after all libraries are loaded.\n */\n function maybeEnableButtons() {\n if (pickerInited && gisInited) {\n document.getElementById('authorize_button').style.visibility = 'visible';\n }\n }\n\n /**\n * Sign in the user upon button click.\n */\n function handleAuthClick() {\n tokenClient.callback = async (response) =\u003e {\n if (response.error !== undefined) {\n throw (response);\n }\n accessToken = response.access_token;\n document.getElementById('signout_button').style.visibility = 'visible';\n document.getElementById('authorize_button').innerText = 'Refresh';\n await createPicker();\n };\n\n if (accessToken === null) {\n // Prompt the user to select a Google Account and ask for consent to share their data\n // when establishing a new session.\n tokenClient.requestAccessToken({prompt: 'consent'});\n } else {\n // Skip display of account chooser and consent dialog for an existing session.\n tokenClient.requestAccessToken({prompt: ''});\n }\n }\n\n /**\n * Sign out the user upon button click.\n */\n function handleSignoutClick() {\n if (accessToken) {\n google.accounts.oauth2.revoke(accessToken);\n accessToken = null;\n document.getElementById('content').innerText = '';\n document.getElementById('authorize_button').innerText = 'Authorize';\n document.getElementById('signout_button').style.visibility = 'hidden';\n }\n }\n\n /**\n * Create and render a Google Picker object for searching images.\n */\n function createPicker() {\n const view = new google.picker.View(google.picker.ViewId.DOCS);\n view.setMimeTypes('image/png,image/jpeg,image/jpg');\n const picker = new google.picker.PickerBuilder()\n .enableFeature(google.picker.Feature.NAV_HIDDEN)\n .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)\n .setDeveloperKey(API_KEY)\n .setAppId(APP_ID)\n .setOAuthToken(accessToken)\n .addView(view)\n .addView(new google.picker.DocsUploadView())\n .setCallback(pickerCallback)\n .build();\n picker.setVisible(true);\n }\n\n /**\n * Displays the file details of the user's selection.\n * @param {object} data - Contains the user selection from the Google Picker.\n */\n async function pickerCallback(data) {\n if (data.action === google.picker.Action.PICKED) {\n let text = `Google Picker response: \\n${JSON.stringify(data, null, 2)}\\n`;\n const document = data[google.picker.Response.DOCUMENTS][0];\n const fileId = document[google.picker.Document.ID];\n console.log(fileId);\n const res = await gapi.client.drive.files.get({\n 'fileId': fileId,\n 'fields': '*',\n });\n text += `Drive API response for first document: \\n${JSON.stringify(res.result, null, 2)}\\n`;\n window.document.getElementById('content').innerText = text;\n }\n }\n\u003c/script\u003e\n\u003cscript async defer src=\"https://apis.google.com/js/api.js\" onload=\"gapiLoaded()\"\u003e\u003c/script\u003e\n\u003cscript async defer src=\"https://accounts.google.com/gsi/client\" onload=\"gisLoaded()\"\u003e\u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nThe `setOAuthToken` function allows an app to use the current auth token to\ndetermine which Google Account the Google Picker uses to display the files. If\na user is signed in with multiple Google Accounts, the Google Picker can\ndisplay the files of the appropriate authorized account.\n\nAfter obtaining the file ID from the Google Picker when opening files, an app\ncan then fetch the file metadata and download the file content as described in\nthe [`get`](/workspace/drive/api/reference/rest/v3/files/get) method of the [`files`](/workspace/drive/api/reference/rest/v3/files) resource."]]