Google Picker code sample

The following code sample shows how to use an image selector or upload page that users can open from a button in a web app.

This example incorporates Google Picker features such as turning on multi-select, hiding the navigation pane, and choosing the user account with the app's current OAuth 2.0 token.

For this example, need to specify several items:

  • To locate both the Client ID and the API Key:

    1. In the Google Cloud console, go to Menu > APIs & Services > Credentials.

      Go to Credentials

  • To locate the App ID:

    1. In the Google Cloud console, go to Menu > IAM & Admin > Settings.

      Go to Settings

    2. Use the Project number for the app ID.

The same Cloud project must contain both the client ID and the app ID as it's used to authorize access to a user's files.

<!DOCTYPE html>
  <title>Picker API Quickstart</title>
  <meta charset="utf-8" />
<p>Picker API API Quickstart</p>

<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize_button" onclick="handleAuthClick()">Authorize</button>
<button id="signout_button" onclick="handleSignoutClick()">Sign Out</button>

<pre id="content" style="white-space: pre-wrap;"></pre>

<script type="text/javascript">
  /* exported gapiLoaded */
  /* exported gisLoaded */
  /* exported handleAuthClick */
  /* exported handleSignoutClick */

  // Authorization scopes required by the API; multiple scopes can be
  // included, separated by spaces.
  const SCOPES = '';

  // TODO(developer): Set to client ID and API key from the Developer Console
  const API_KEY = '<YOUR_API_KEY>';

  // TODO(developer): Replace with your own project number from
  const APP_ID = '<YOUR_APP_ID>';

  let tokenClient;
  let accessToken = null;
  let pickerInited = false;
  let gisInited = false;

  document.getElementById('authorize_button').style.visibility = 'hidden';
  document.getElementById('signout_button').style.visibility = 'hidden';

   * Callback after api.js is loaded.
  function gapiLoaded() {
    gapi.load('client:picker', initializePicker);

   * Callback after the API client is loaded. Loads the
   * discovery doc to initialize the API.
  async function initializePicker() {
    await gapi.client.load('');
    pickerInited = true;

   * Callback after Google Identity Services are loaded.
  function gisLoaded() {
    tokenClient = google.accounts.oauth2.initTokenClient({
      client_id: CLIENT_ID,
      scope: SCOPES,
      callback: '', // defined later
    gisInited = true;

   * Enables user interaction after all libraries are loaded.
  function maybeEnableButtons() {
    if (pickerInited && gisInited) {
      document.getElementById('authorize_button').style.visibility = 'visible';

   *  Sign in the user upon button click.
  function handleAuthClick() {
    tokenClient.callback = async (response) => {
      if (response.error !== undefined) {
        throw (response);
      accessToken = response.access_token;
      document.getElementById('signout_button').style.visibility = 'visible';
      document.getElementById('authorize_button').innerText = 'Refresh';
      await createPicker();

    if (accessToken === null) {
      // Prompt the user to select a Google Account and ask for consent to share their data
      // when establishing a new session.
      tokenClient.requestAccessToken({prompt: 'consent'});
    } else {
      // Skip display of account chooser and consent dialog for an existing session.
      tokenClient.requestAccessToken({prompt: ''});

   *  Sign out the user upon button click.
  function handleSignoutClick() {
    if (accessToken) {
      accessToken = null;
      document.getElementById('content').innerText = '';
      document.getElementById('authorize_button').innerText = 'Authorize';
      document.getElementById('signout_button').style.visibility = 'hidden';

   *  Create and render a Picker object for searching images.
  function createPicker() {
    const view = new google.picker.View(google.picker.ViewId.DOCS);
    const picker = new google.picker.PickerBuilder()
        .addView(new google.picker.DocsUploadView())

   * Displays the file details of the user's selection.
   * @param {object} data - Containers the user selection from the picker
  async function pickerCallback(data) {
    if (data.action === google.picker.Action.PICKED) {
      let text = `Picker response: \n${JSON.stringify(data, null, 2)}\n`;
      const document = data[google.picker.Response.DOCUMENTS][0];
      const fileId = document[google.picker.Document.ID];
      const res = await{
        'fileId': fileId,
        'fields': '*',
      text += `Drive API response for first document: \n${JSON.stringify(res.result, null, 2)}\n`;
      window.document.getElementById('content').innerText = text;
<script async defer src="" onload="gapiLoaded()"></script>
<script async defer src="" onload="gisLoaded()"></script>

The setOAuthToken function allows an app to use the current auth token to determine which Google Account the Google Picker uses to display the files. If a user is signed in with multiple Google Accounts, the Google Picker can display the files of the appropriate authorized account.

After obtaining the file ID from the Google Picker when opening files, an application can then fetch the file metadata and download the file content as described in the reference documentation for files.get.