Hitesh Raghuvanshi
Hitesh Raghuvanshi

Reputation: 73

Permission exception while inserting file in google drive rest api java

I am trying to upload files to google drive using drive rest api, following this tutorial.

For authorization, function is :

 public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in =
        DriveQuickstart.class.getResourceAsStream("client_secret.json");
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver()).authorize("user");
    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

My function for getting all files present in drive works perfectly fine.

private static void listFiles(Drive service) throws IOException
{
     // Print the names and IDs for up to 10 files.
    FileList result = service.files().list().setMaxResults(10).execute();
    List<File> files = result.getItems();
    if (files == null || files.size() == 0) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getTitle(), file.getId());
        }
    }
}

But when I try to create and insert files in drive I get exception,function and exception are as follows:

private static void insertFile(Drive service, String title, String description) {
      // File's metadata.
      File file = new File();
      file.setTitle(title);
      file.setDescription(description);
      file.setMimeType("application/vnd.google-apps.drive-sdk");

      try {
        file = service.files().insert(file).execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


      // Print the new file ID.
      System.out.println("File ID: %s" + file.getId());
    }

Exception is:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden

{
  "code" : 403,
  "errors" : [ {
  "domain" : "global",
  "message" : "Insufficient Permission",
  "reason" : "insufficientPermissions"
  } ],
 "message" : "Insufficient Permission"
}

File ID: %snull at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) at com.teamchat.google.sheet.integration.DriveQuickstart.insertFile(DriveQuickstart.java:137) at com.teamchat.google.sheet.integration.DriveQuickstart.main(DriveQuickstart.java:110)

This is just a simple java project and using OAuth.

So I am able to get list of files but not able to insert files in google drive. Can someone tell me what I am doing wrong.

Upvotes: 0

Views: 2167

Answers (2)

Tarun Rawat
Tarun Rawat

Reputation: 254

The problem is with Scope

Change this line to

private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);

to

private static final List<String> SCOPES = new ArrayList(DriveScopes.all());"

or change value of scopes according to your need.Read this

and remember to delete token folder and re run the app again.

Upvotes: 0

Kalem
Kalem

Reputation: 1132

"reason" : "insufficientPermissions"

Which means that the scopes you set when getting authorization doesn't allow you to write on the resource.

Set the correct scopes https://developers.google.com/drive/web/scopes or https://developers.google.com/resources/api-libraries/documentation/drive/v2/java/latest/com/google/api/services/drive/DriveScopes.html

Usually :

private static final List<String> SCOPES =
        Arrays.asList(DriveScopes.DRIVE_FILE);

Upvotes: 5

Related Questions