如何使用Google Drive Api管理用户文档权限设置? (JavaScript)的

时间:2013-04-19 06:06:15

标签: javascript google-drive-api google-api-js-client

我是Google Drive SDK的新手。  我跟着

https://developers.google.com/drive/v2/reference/permissions/insert?hl=en

这个示例显示了如何更改文件的权限。我能够运行这个代码,但我的问题是我想要更改多个文件的权限。我为用户尝试了一些循环,并在其中的fileId循环,但因为javascript不等待子函数完成(异步调用函数),它不会为我工作

请帮帮我

以下是我的代码...... 我很困惑,请帮我理清这个

此代码请在此纠正我

      var scopes = 'https://www.googleapis.com/auth/drive';

      // Use a button to handle authentication the first time.
      function handleClientLoad() {
        gapi.client.setApiKey(apiKey);
        //window.setTimeout(checkAuth,1);
        checkAuth();
      }

      function checkAuth() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
      }

      function handleAuthResult(authResult) {
        var authorizeButton = document.getElementById('authorize-button');
        if (authResult && !authResult.error) {
          authorizeButton.style.visibility = 'hidden';
          makeApiCall();
        } else {
          authorizeButton.style.visibility = '';
          authorizeButton.onclick = handleAuthClick;
        }
      }

      function handleAuthClick(event) {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        return false;
      }

      // Load the API and make an API call.  Display the results on the screen.
      function makeApiCall() {
        var fileID = new Array();
        var email = new Array();
        fileID[0] = "1fEYDfB9owJAdxQ7lI0";
        fileID[1] = "1YYcKn1ZsiPYWA";

        email[0] = "email1@email.com";
        email[1] = "email2@email.com";
        email[2] = "email3@email.com";

          gapi.client.load('drive', 'v2', function() {
              for(var i=0;i<email.length;i++)
              {
                  var body = {
                            'value': email[i],
                            'type': "user",
                            'role': "reader"
                          };
                  for(var j=0;j<fileID.length;j++)
                    {

                        var fileid = fileID[j];

                       excuteRequest(body, fileid, function() { 
                              var request = gapi.client.drive.permissions.insert({
                                    'fileId': fileid,
                                    'resource': body
                                  });
                            request.execute(function(resp) { });
                        }); 
                    }
              }
          });
    }

      function excuteRequest(param1, param2, callback) {

            if (callback && typeof(callback) === "function") {
                callback();
            }
        }

1 个答案:

答案 0 :(得分:3)

使用流程控制库,类似于asynchttps://github.com/caolan/async

var files = ["id1", "id2", "id3"];
async.each(files, function(fileId, callback){

  gapi.client.drive.permissions.insert({
     'fileId': fileId,
     'resource': body
  }).execute(function(response) {
      callback(null, response);
  });

}, function(err){
  if (!err) { console.log('done.'); }
});