Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

embed google drive picker as a part of page

I want to embed/implement Google Drive as a part of my page; like a normal grid or a table instead of having as a popup. I have took a reference from GoogleAPI page. Also, researched many things for my requirements but nothing would worked for me.

Here is the javascript code that I am using

// The Browser API key obtained from the Google API Console.
// Replace with your own Browser API key, or your own key.
var developerKey = 'xxxxxxxxxxxxxx';

// The Client ID obtained from the Google API Console. Replace with your own Client ID.
var clientId = "xxxxxxxxxxxx.apps.googleusercontent.com"

// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = "xxxxxxxxxxxx";

// Scope to use to access user's Drive items.
var scope = ['https://www.googleapis.com/auth/drive'];

var pickerApiLoaded = false;
var oauthToken;
var picker;

// Use the Google API Loader script to load the google.picker script.
function loadPicker() {

  gapi.load('auth', {
    'callback': onAuthApiLoad
  });
  gapi.load('picker', {
    'callback': onPickerApiLoad
  });
}

function onAuthApiLoad() {

  window.gapi.auth.authorize({
      'client_id': clientId,
      'scope': scope,
      'immediate': false
    },
    handleAuthResult);
}

function onPickerApiLoad() {

  pickerApiLoaded = true;
  createPicker();
}

function handleAuthResult(authResult) {

  if (authResult && !authResult.error) {
    oauthToken = authResult.access_token;
    createPicker();
  }
}

// Create and render a Picker object for searching images.
function createPicker() {

  if (pickerApiLoaded && oauthToken) {
    var view = new google.picker.DocsView()
      .setIncludeFolders(true)
      .setOwnedByMe(true);
    picker = new google.picker.PickerBuilder()
      .enableFeature(google.picker.Feature.NAV_HIDDEN)
      .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
      .setAppId(appId)
      .setOAuthToken(oauthToken)
      .addView(view)
      .addView(new google.picker.DocsUploadView().setIncludeFolders(true))
      .setDeveloperKey(developerKey)
      .setCallback(pickerCallback)
      .build();
    picker.setVisible(true);
  }
}

// A simple callback implementation.
function pickerCallback(data) {

  if (data.action == google.picker.Action.PICKED) {
    var fileId = data.docs[0].id;
    alert('The user selected: ' + fileId);
  }
}
<button onclick="loadPicker(); return false;">Pick From Google Drive</button>
<div id="result"></div>

<!-- The Google API Loader script. -->
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
like image 303
Ishan Malik Avatar asked May 10 '17 10:05

Ishan Malik


People also ask

What is the file picker icon in Google Slides?

The Google Picker is a "File Open" dialog for information stored on Google servers. You can use the Google Picker API to allow users to open or upload Google Drive files. Note: To allow users to open Drive files from a mobile app, refer to Google Workspace APIs for Android or Google Workspace APIs for iOS.

How do I enable Google picker API?

Enable the Google Picker API If you haven't done so already, create your application's API key by clicking Create credentials > API key. Next, look for your API key in the API keys section. If you haven't done so already, create your OAuth 2.0 credentials by clicking Create credentials > OAuth client ID.

Where is file picker in Google Docs?

Click the Open file picker icon in the Recent Documents section. Click Upload at the top of the file picker window and drag the file you want to upload into the window when prompted.


2 Answers

Use PickerBuilder.toUri() instead of PickerBuilder.build(). It will return picker url and set this to iframe.

like image 133
Mohin Mathakia Avatar answered Sep 23 '22 17:09

Mohin Mathakia


According to the issue reported here, gapi.auth is deprecated. You should use gapi.auth2 instead.

From Google Developers

Use,

gapi.auth2.init({
    client_id: 'CLIENT_ID.apps.googleusercontent.com',
    scope : scope ,
});

and it will return a Promise

gapi.auth2.GoogleAuth

A full reference can be seen in the Google Developer Page

like image 25
Sagar V Avatar answered Sep 23 '22 17:09

Sagar V