Google Drive credential returns 403 insufficientPermissions

68 Views Asked by At

I'm trying to get a list of files' data from Drive. Following the google's example didn't work.

This is an example of my code:

private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

private Drive createDriveService() throws IOException, GeneralSecurityException
    {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        InputStream in = ImageImpl.class.getResourceAsStream(USER_CREDENTIALS_FILE_PATH);
        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, Collections.singleton(DriveScopes.DRIVE))
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
        return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(applicationName)
                .build();
    }

    public void listFiles() throws IOException, GeneralSecurityException
    {
        Drive service = createDriveService();
        FileList result = service.files().list()
                .setPageSize(10)
                .setFields("nextPageToken, files(id, name)")
                .execute();
        List<File> files = result.getFiles();
    }

Output:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
GET https://www.googleapis.com/drive/v3/files?fields=nextPageToken,%20files(id,%20name)&pageSize=10
{
  "code": 403,
  "details": [
    {
      "@type": "type.googleapis.com/google.rpc.ErrorInfo",
      "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
      "domain": "googleapis.com",
      "metadata": {
        "service": "drive.googleapis.com",
        "method": "google.apps.drive.v3.DriveFiles.List"
      }
    }
  ],
  "errors": [
    {
      "domain": "global",
      "message": "Insufficient Permission",
      "reason": "insufficientPermissions"
    }
  ],
  "message": "Request had insufficient authentication scopes.",
  "status": "PERMISSION_DENIED"
}

Any ideas?

I've tried test the Query here -> https://developers.google.com/drive/api/reference/rest/v2/files/list and works ok, but not locally Also, I'm using the same credential to have access to a Sheet by ID and works, so that tells me the authentication it's fine, the only difference is the scope.

1

There are 1 best solutions below

3
Linda Lawton - DaImTo On

go to TOKENS_DIRECTORY_PATH And delete the file there.

You previously authorized your application then changed the scope.

Your app needs to request authorization of the user again and request the new scope.