我正在创建一个录音机,在完成录制后,自动将录制内容上传到用户的Google云端硬盘并与预定义的联系人共享其链接。我已经能够使用Google Drive Android API
上传文件了。现在,我想分享上传文件的链接。我已经能够上传文件drive id
和resourceId
,我可以使用该webContentLink
文件获取private
。但是,默认情况下此文件具有anyone with link
访问权限。如何使用Google Drive Android API
或Google Drive REST API
?
private void changePermissionSettings(String resourceId) throws GeneralSecurityException, IOException, URISyntaxException {
com.google.api.services.drive.Drive driveService = getDriveService();
JsonBatchCallback<com.google.api.services.drive.model.Permission> callback = new JsonBatchCallback<com.google.api.services.drive.model.Permission>() {
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
Log.e("upload", "Permission Setting failed");
}
@Override
public void onSuccess(com.google.api.services.drive.model.Permission permission, HttpHeaders responseHeaders) throws IOException {
Log.e("upload", "Permission Setting success");
}
};
BatchRequest batchRequest = driveService.batch();
com.google.api.services.drive.model.Permission userPermission = new com.google.api.services.drive.model.Permission()
.setType("user")
.setRole("writer");
driveService.permissions().create(resourceId, userPermission)
.setFields("id")
.queue(batchRequest, callback);
com.google.api.services.drive.model.Permission contactPermission = new com.google.api.services.drive.model.Permission()
.setType("anyone")
.setRole("reader");
driveService.permissions().create(resourceId, contactPermission)
.setFields("id")
.queue(batchRequest, callback);
batchRequest.execute();
}
private com.google.api.services.drive.Drive getDriveService() throws GeneralSecurityException,
IOException, URISyntaxException {
Collection<String> elenco = new ArrayList<>();
elenco.add("https://www.googleapis.com/auth/drive");
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, elenco)
.setSelectedAccountName(getAccountName());
Log.e("upload", getAccountName());
return new com.google.api.services.drive.Drive.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential).build();
}
我一直在尝试使用以下代码更改权限设置:
2,2 23.45,6 45,21.65 2,2
但是这段代码不起作用。我做错了什么?