Killkase42
Killkase42

Reputation: 13

How to solve 403 User Permissions error with Google Drive API (Java)

I recently started using the Google Drive API and have successfully run the quickstart program provided on the Google Drive API page.

The code I used is under "Step 3". When I tried to implement the feature of creating a new file on my Google Drive, however, I received

403 "Insufficient Permission: Request had insufficient authentication scopes.".

The code I used for this is:

fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id")
    .execute();
System.out.println("File ID: " + file.getId());

While searching the Google Drive API page for a solution, I found https://developers.google.com/drive/api/v3/handle-errors#resolve_a_403_error_the_user_does_not_have_sufficient_permissions_for_file_fileid, but this did not help.

Other than the above link, I found a few sources saying I need to integrate oAuth or oAuth 2.0, but no Java syntax or instructions on how to do that.

I'm very likely making a simple error, but then again I'm new to Google APIs and would appreciate any help. Thank you

Upvotes: 1

Views: 1228

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117261

The quick start you are following asks the user to consent to

DriveScopes.DRIVE_METADATA_READONLY

this means that you have only read only access.

Files.create requires write access. you need to change your code to include the higher scope

DriveScopes.DRIVE

then go find TOKENS_DIRECTORY_PATH and delete the token file for the user which should cause your code to request authorization again and show a higher level of scope in the consent screen

Upvotes: 1

Related Questions