![](/img/trans.png)
[英]How Do i check permission of a folder to a user in Google Drive SDK?
[英]Query Google Drive folder and change sharing permission
Google Drive API 的新手,我想知道您是否可以查詢 Google Drive 文件夾並批量更改共享權限。 我工作的公司一直在外部共享文件,但沒有人跟蹤這些文件。 一旦項目被歸檔,我們要確保沒有任何文件仍然被共享。 我一直在檢查 API 文檔,但只能找到我想要實現的點點滴滴。 有人可以指出我正確的方向嗎? 我確定這一定是以前做過的......
親切的問候!
編輯:
這是我現在擁有的代碼。 根據我下面的評論,我收到一個 Gradle 錯誤,說使用了不推薦使用的 Gradle 功能。
import com.google.api.client.auth.oauth2.Credential;
importcom.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalleApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
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");
}
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();
// Print the names and IDs for up to 10 files.
FileList result = service.files().list()
.setFields("nextPageToken, files(id, name)")
.setQ("'Folder ID'")
.execute();
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("-----------------------------------");
}
}
}
您可以列出Drive 文件的權限,也可以遍歷文件夾的文件。 然后您需要獲取每一項 Permissions 以檢索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("-----------------------------------");
}
}
}
我希望這是有幫助的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.