Skip to content
This repository was archived by the owner on Jan 11, 2022. It is now read-only.

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

Closed
erpriyesh opened this issue Sep 24, 2020 · 1 comment
Closed

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

erpriyesh opened this issue Sep 24, 2020 · 1 comment

Comments

@erpriyesh
Copy link

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
Copy link
Author

erpriyesh commented Sep 24, 2020

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

}
`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant