我想開發一個應用程序,其中我使用谷歌驅動器上傳和共享文件在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
?? 「