0

I am trying to develop an app wherein I am using Google Drive to upload and share of file in service. However, the problem is that shared link has private access. I want to change that access to anyone with link. To achieve that I am using Google Drive REST API, however nothing seems to happen when I execute the code. Here is my code:

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

I have two questions here:

  1. Is it right to use both Google Drive Android API and Google Drive REST API together??

  2. How to change permission setting of file from private to anyone with link?? Documentation mentions that Authorization happens in response to receiving an error when sending a request. How can that be achieved in service??

Vaibhav Agarwal
  • 975
  • 1
  • 7
  • 22

1 Answers1

0

"Is it right to use both Google Drive Android API and Google Drive REST API together??"

As stated here, it is possible. "Just looking at the this Lifecycle of a Drive file, you can imagine your app on top of that picture (Android App) and the REST API on the bottom (Drive Service)."

There is a few points to keep in mind, though:

  1. The GDAA's main identifier, the DriveId lives in GDAA (GooPlaySvcs) only and does not exist in the REST Api. You must retrieve 'ResourceId' which is the main identifier in the REST Api (see SO 29030110).

  2. ResourceId can be obtained from the DriveId only after GDAA committed (uploaded) the file/folder (see SO 22874657)

  3. You will run into a lot of timing issues caused by the fact that GDAA 'buffers' network requests on it's own schedule (system optimized), whereas the REST Api let your app control the waiting for the response. In general, if you scan these SO questions, you'll find a lot of chatter about these issues (it's a mess, though).

I maintain a minimal CRUD wrappers for both GDAA and the REST API that can help you if you merge them (the MainActivity in both of them is almost identical and the CRUD methods have the same signatures).

"How to change permission setting of file from private to anyone with link?? Documentation mentions that Authorization happens in response to receiving an error when sending a request. How can that be achieved in service??"

Based from this thread, you have to set the following permissions:

private Permission insertPermission(Drive service, String fileId) throws Exception{
   Permission newPermission = new Permission();
   newPermission.setType("anyone");
   newPermission.setRole("reader");
   newPermission.setValue("");
   newPermission.setWithLink(true);
   return service.permissions().insert(fileId, newPermission).execute();
}

You can also check this link: Google Drive SDK - change item sharing permissions.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59