是Google Drive API的新功能,我想知道您是否可以查询Google Drive文件夹并批量更改共享权限。我工作的公司一直在外部共享文件,但是没有人跟踪文件。项目归档后,我们要确保没有文件仍然共享。我一直在检查API文档,但只能找到我想要实现的点点滴滴。有人可以指出我正确的方向吗?我敢肯定这必须已经做过.....
亲切的问候!
您可以list驱动器文件的权限,也可以iterate浏览文件夹的文件。然后,您将需要获取每个权限,以便检索EmailAddress
和Role
。
我假设您正在使用Java,因此如果我们看一下Quickstart,我们可以使用它来设置脚本的凭据和令牌,而忽略其余的代码。
public class DrivePerms {
private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = DrivePerms.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
注意,我省略了“导入”,您可以使用快速入门中的导入。此外,范围设置为。Drive而不是。DRIVE_METADATA_READONLY。
在主类中,我声明了HTT_TRANSPORT和Drive服务(就像在快速入门中一样:
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
然后:
FileList result = service.files().list()
.setFields("nextPageToken, files(id, name)")
.setQ("'your folder Id' in parents")
.execute();
List<File> files = result.getFiles();
for (File file : files){
System.out.println("File: " + file.getName());
PermissionList result2 = service.permissions().list(file.getId())
.execute();
List<Permission> permsList = result2.getPermissions();
for (Permission perms : permsList){
Permission roles = service.permissions().get(file.getId(), perms.getId())
.setFields("emailAddress, role")
.execute();
System.out.println("Email: " + roles.getEmailAddress());
System.out.println("Role: " + roles.getRole());
System.out.println("-----------------------------------");
}
}
}
我希望这会有所帮助!