简体   繁体   中英

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.

在此输入图像描述

  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 ?

您是否使用API​​控制台中的实际值替换上面代码中的MY_APP_IDMY_API_KEYMY_CLIENT_ID占位符?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM