3

I am creating an audio recorder which after completion of recording, automatically uploads recording to user's Google Drive and share its link with predefined set of contacts. I have been able to upload the file using Google Drive Android API. Now, I want to share uploaded file's link. I have been able to get uploaded file's drive id and resourceIdusing which I can get webContentLink of the file. However, this file by default has private access. How can I change this access to anyone with link programmatically using either Google Drive Android API or Google Drive REST API??

I have been trying to change permission settings using following code:

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

But this code is not working. What am I doing wrong??

Vaibhav Agarwal
  • 975
  • 1
  • 7
  • 22
  • Using the Drive REST API, try adding permission to the file using [Permissions.create](https://developers.google.com/drive/v3/reference/permissions/create). It has a property role and type. Set type to 'anyone' so you can share it publicly and set allowFileDiscovery property to false so your permissions won't change but file would be shareable by a link. – ReyAnthonyRenacia Oct 22 '16 at 23:34
  • hey @noogui, I tried doing that using the code I added to this question. But it is not working. Can you please help me identify where I am going wrong?? – Vaibhav Agarwal Oct 23 '16 at 03:50

0 Answers0