我正在尝试在Google Apps脚本中使用他/她的电子邮件ID来获取已暂停用户的文件,我已经创建了一个服务帐户,并使用服务帐户身份验证实现了一个应用脚本API,为此我使用了{{3 }}尽管我试图通过api调用来“模拟”用户,但我只能访问该用户的公共文件,我正在尝试访问所有文件(包括私有和公共),也许我知道我在做什么错了?
/**
* Entrypoint
*/
var PRIVATE_KEY = 'PPRIVATE_KEY goes in here';
var CLIENT_EMAIL = 'CLIENT_EMAIL goes in here';
function test() {
fetchUser();
}
/**
* Fetches the suspended user details from the AdminDirectory.
*/
function fetchUser() {
// Set the constant options only once.
const options = {
domain: 'xyz.com',
orderBy: 'email',
query: 'isSuspended=true',
maxResults: 500,
fields: "nextPageToken,users"
};
// Could log the options here to ensure they are valid and in the right format.
const results = [];
do {
var search = AdminDirectory.Users.list(options);
// Update the page token in case we have more than 1 page of results.
options.pageToken = search.nextPageToken;
// Append this page of results to our collected results.
if(search.users && search.users.length)
Array.prototype.push.apply(results, search.users);
} while (options.pageToken);
//Logger.log(results);
for(var k = 0; k < results.length; k++){
var fullEmail = results[k].primaryEmail;
//Logger.log(fullEmail);
fetchAllFilesOwnedByEmail(fullEmail);
}
}
/**
* Fetch all files based on the user email id
*/
function fetchAllFilesOwnedByEmail(email) {
var service = getService(email);
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/drive/v2/files?pageSize=1';
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
console.log("gs test1");
Logger.log(JSON.stringify(result, null, 2));
} else {
console.log("gs test1");
Logger.log(service.getLastError());
}
const searchParams = {
corpora: 'domain',
orderBy: 'createdDate',
q: "'" + email + "' in owners",
fields: 'nextPageToken,items(id,title,mimeType,userPermission)'
};
Logger.log('searchParams:' +searchParams);
const results = [];
do {
var search = Drive.Files.list(searchParams);
if (search.items)
Array.prototype.push.apply(results, search.items);
searchParams.pageToken = search.nextPageToken;
} while (searchParams.pageToken);
//return results;
Logger.log('Files:' +results);
}
/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
var service = getService();
service.reset();
}
/**
* Configures the service.
*/
function getService(userEmail) {
return OAuth2.createService('GoogleDrive:' + userEmail)
// Set the endpoint URL.
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the private key and issuer.
.setPrivateKey('PRIVATE_KEY goes in here')
.setIssuer('CLIENT_EMAIL goes in here')
// Set the name of the user to impersonate. This will only work for
// Google Apps for Work/EDU accounts whose admin has setup domain-wide
// delegation:
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
.setSubject(userEmail)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scope. This must match one of the scopes configured during the
// setup of domain-wide delegation.
.setScope('https://www.googleapis.com/auth/drive');
}