Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access google drive file content from JavaScript

I have

I have a web/html5 applicaton where I provide to a user a functionality to visualize authenticated user's Google Drive content files and pick them.

I use with success google.picker UI interface and it works pretty well. Example:

   var view = new google.picker.View(google.picker.ViewId.DOCS);

    var picker = new google.picker.PickerBuilder()
      .enableFeature(google.picker.Feature.NAV_HIDDEN)
      .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
      .setAppId('MY_CLIENT_ID')       
      .addView(view)
      .setCallback(pickerCallback)
      .build();
    picker.setVisible(true);

Problem

One time user picked the file(s) from the UI, the pickerCallback is called and I get a list of object properties selected by the user.

enter image description here

  1. I see driveError: "ERROR" and driveSuccess: false
  2. After I'm not able more to download that file using gapi

My attempt was like:

        gapi.client.setApiKey('MY_API_KEY');
        gapi.client.load('drive', 'v2', function () {

            var scopes = 'https://www.googleapis.com/auth/drive';
            gapi.auth.authorize({ client_id: "MY_CLIENT_ID", scope: scopes, immediate: true },
            function () {

                //TOKEN IS ALWAYS NULL !! 
                var myToken = gapi.auth.getToken();
                gapi.client.request({
                    'path': '/drive/v2/files/' + file.id,
                    'method': 'GET',
                    callback: function (theResponseJS, theResponseTXT) {

                        var myXHR = new XMLHttpRequest();
                        myXHR.open('GET', theResponseJS.downloadUrl, true);   
                        myXHR.onreadystatechange = function (theProgressEvent) {
                            if (myXHR.readyState == 4) {
                                if (myXHR.status == 200) {
                                    //200=OK
                                    console.log(myXHR.response);
                                }
                            }
                        }
                        myXHR.send();
                    }
                });
            }

            );

        });

Here is something to do with authentication, imo, but according to the latest posts from Google itself, TOKEN management was deprecated and actually removed.

Question

How can I read a content of the picked by the user file form Google Drive ?

like image 319
Tigran Avatar asked Feb 01 '26 22:02

Tigran


1 Answers

Are you replacing the MY_APP_ID, MY_API_KEY and MY_CLIENT_ID placeholders in your code above with real values from the APIs Console?

like image 161
Claudio Cherubino Avatar answered Feb 03 '26 13:02

Claudio Cherubino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!