0

I am trying to list all shared drives from googledrive using List drive java method. this is my code.

public static void getDriveId(String folderName, String oauthToken) throws Exception {
            DriveList list ;
            String pageToken = null;
            GoogleCredential credential = new GoogleCredential(); 
            credential.setAccessToken(oauthToken);
            try {
                _driveService = new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential).setApplicationName(appName).build();
            } catch (GeneralSecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            list = _driveService.drives().list().setQ(folderName).setFields("nextPageToken, drives(id, name)")
                 .execute();
        System.out.println(list.toString());
        }

I am setting Q parameter in query where foldername= "salesDrive"

Its giving me error like

Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request {

  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "q",
    "locationType" : "parameter",
    "message" : "Invalid Value",
    "reason" : "invalid"
  } ],
  "message" : "Invalid Value"
}

How can i set this q parameter inorder to display only specific shared drive details?

Daniel
  • 79
  • 1
  • 10

1 Answers1

0

You are missing two things:

  1. It is not enough to just provide foldername, you should specify name = 'foldername' (please keep in mind that it should the name of the shared Drive, not a folder contained in this Drive)
  2. As mentioned in the documentation:

To search for a specific set of shared drives, set the useDomainAdminAccess parameter to true

Missing out on any of the steps above will result in an 400 error, as the one you received.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • I have tried the foldername as name Contains 'Salesdrive' and name = 'Salesdrive' but its giving me invalid parameter error – Daniel Nov 09 '20 at 12:09
  • Did you set `useDomainAdminAccess` to `true`? https://developers.google.com/drive/api/v3/reference/drives/list https://developers.google.com/drive/api/v3/search-shareddrives – ziganotschka Nov 09 '20 at 13:03
  • yep added it, but still it gives me error Caused by: list = _driveService.drives().list().setQ(folderName).setUseDomainAdminAccess(true).setFields("nextPageToken, drives(id, name)") .setPageToken(pageToken).execute(); Error: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request { "code" : 400, "errors" : [ { "domain" : "global", "location" : "q", "locationType" : "parameter", "message" : "Invalid Value", "reason" : "invalid" } ], "message" : "Invalid Value" } – Daniel Nov 09 '20 at 13:12
  • What about `_driveService.drives().list().setQ("fullText = 'folderName' ").setUseDomainAdminAccess(true)`? – ziganotschka Nov 09 '20 at 13:19
  • list = _driveService.drives().list().setQ("fullText = 'folderName' ").setUseDomainAdminAccess(true).setFields("nextPageToken, drives(id, name)") .setPageToken(pageToken).execute(); No luck its throwing same error. – Daniel Nov 09 '20 at 13:32
  • Do you experience the same problem when testing with the "Try this API"? – ziganotschka Nov 09 '20 at 13:35
  • No its working perfectly in api console, giving me error only in java method or in postman – Daniel Nov 09 '20 at 13:39
  • And it does it work in Java / postman if you do not set Q? – ziganotschka Nov 09 '20 at 14:13
  • sounds like you might have a syntax error... try to hardcode the value for `folderName` and doublecheck that you have the quotes right. – ziganotschka Nov 09 '20 at 14:45