我正在使用GAS_Service_Account library。
我能够进行所有.p12
密钥解码。但现在我有:
myJwt.generateJWT().requestToken();
var token=myJwt.getToken();
现在我想知道一种使用它来进行API调用的简单方法 服务帐户到api like驱动器以获取特定用户的文件。
答案 0 :(得分:3)
服务帐户是强大的工具。 GersonLobos提到的图书馆是:MJ5317VIFJyKpi9HCkXOfS0MLm9v2IJHf
让我们回顾一下。请参阅本视频的前半部分,了解如何在您的域中设置服务帐户: https://www.youtube.com/watch?v=EDmEzZEGSts
在项目中启用库后,您可以使用以下内容生成令牌:
// If userEmail is null the service account's token is returned
function tokenService(userEmail){
var userEmail = userEmail || ""
var jsonKey = JSON.parse(PropertiesService.getScriptProperties().getProperty("jsonKey"));
var privateKey = jsonKey.private_key;
var serviceAccountEmail = jsonKey.client_email;
if(!userEmail){userEmail = serviceAccountEmail};
var sa = GSApp.init(privateKey, ['https://www.googleapis.com/auth/drive'], serviceAccountEmail).addUser(userEmail);
var tokenObj = JSON.parse(PropertiesService.getScriptProperties().getProperty(userEmail)) || {};
return function(){
var nowTime = parseInt((Date.now()/1000).toString().substr(0,10));
if(!("token" in tokenObj) || tokenObj.expire < nowTime){
var newToken = sa.requestToken().getToken(userEmail);
PropertiesService.getScriptProperties().setProperty(userEmail, JSON.stringify(newToken));
tokenObj.token = newToken.token;
tokenObj.expire = newToken.expire;
}
return tokenObj.token;
}
}
现在你有了你的令牌。我甚至展示了如何缓存它,因为请求需要几秒钟。现在你可以打个电话了。根据Gerson的要求,这里有一些驱动器电话:
function transferFileToUser(fileId, transferToEmail, token){
var url = "https://www.googleapis.com/drive/v2/files/"+fileId+"/permissions?sendNotificationEmails=false";
var payload = {"role":"owner","value":transferToEmail,"type":"user"};
var params ={method:"POST",
contentType:'application/json',
headers:{Authorization: "Bearer " + token},
payload:JSON.stringify(payload),
muteHttpExceptions:true
};
var results = UrlFetchApp.fetch(url, params);
return JSON.parse(results.getContentText());
}
function getAllFolders(token){
var query = "mimeType = 'application/vnd.google-apps.folder'";
return driveList(query, token);
}
function getFilesInFolder(folderId, token){
var query = "'"+folderId+"' in parents and mimeType != 'application/vnd.google-apps.folder'";
return driveList(query, token);
}
// genereic call to drive just pass the query
function driveList(query, token){
var filesArray = [];
var pageToken = "";
var query = encodeURIComponent(query);
var params = {method:"GET",
contentType:'application/json',
headers:{Authorization:"Bearer "+token},
muteHttpExceptions:true
};
var url = "https://www.googleapis.com/drive/v2/files?q="+query;
do{
var results = UrlFetchApp.fetch(url,params);
if(results.getResponseCode() != 200){
Logger.log(results);
break;
}
var files = JSON.parse(results.getContentText());
url = "https://www.googleapis.com/drive/v2/files?q="+query;
for(var i in files.items){
filesArray.push({"name":files.items[i].title, "id":files.items[i].id})
}
pageToken = files.nextPageToken;
url += "&pageToken="+encodeURIComponent(pageToken);
}while(pageToken != undefined)
var filesObj = {};
filesObj["fileObjs"] = filesArray;
return filesObj;
}