gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:47:51 30 4
gpt4 key购买 nike

我是 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();
}
}

最佳答案

使用流程控制库,类似于async:https://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.'); }
});

关于javascript - 如何使用 Google Drive Api 管理用户文档权限设置? (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16098273/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com