googleworkspace / android-samples

Android samples for Google Workspace APIs
Apache License 2.0
636 stars 410 forks source link

How to create a shareable folder programatically in google drive #130

Closed erpriyesh closed 4 years ago

erpriyesh commented 4 years ago

I am uploading all call recordings to google drive and saving its link to database but that link is only accessible if my own account is opened so i want to make that folder shareable so any one can access that recording with that link.

So how to make any folder public shareable.

Thanks in Advance!

erpriyesh commented 4 years ago

Got the answer of this. using this code snippet we can make any folder or file public : anyone with link can access

` private final Executor executor = Executors.newSingleThreadExecutor();

private void changePermissionSettings(String fileId) throws GeneralSecurityException, IOException, URISyntaxException {

    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");
        }
    };

    Tasks.call(executor, () -> {
        BatchRequest batchRequest = googleDriveService.batch();

        com.google.api.services.drive.model.Permission userPermission = new com.google.api.services.drive.model.Permission()
                .setType("user")
                .setRole("writer");

        googleDriveService.permissions().create(fileId, 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");

        googleDriveService.permissions().create(fileId, contactPermission)
                .setFields("id")
                .queue(batchRequest, callback);

        batchRequest.execute();

        return "";
    });

} `