我想开发一个应用程序,其中我使用谷歌驱动器上传和共享文件在service
。但是,问题是共享链接有private
访问权限。我想改变对anyone with link
的访问权限。为了实现我使用Google Drive REST API
,但是当我执行代码时似乎没有发生。这里是我的代码:连接到谷歌API后台服务的Java客户端
public void changePermissionSettings(String resourceId) throws GeneralSecurityException, IOException, URISyntaxException {
com.google.api.services.drive.Drive driveService = getDriveService();
JsonBatchCallback<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");
}
};
final BatchRequest batchRequest = driveService.batch();
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);
new Thread(new Runnable() {
@Override
public void run() {
try {
batchRequest.execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
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());
return new com.google.api.services.drive.Drive.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential).build();
}
我有两个问题在这里:
那么,是否可以同时使用谷歌云端硬盘Android API和谷歌驱动器REST API一起?
如何将文件的权限设置从
private
更改为anyone with link
? Documentation提到Authorization happens in response to receiving an error when sending a request.
如何才能实现service
?? “