2

I have created a service account in google drive for uploading files in google drive from server and download those files later. But when I tried to get list of files of a folder and get the webContentLinks of those file, it returned null. I checked it for some other properties, it returns null for both getWebContentLink() and getWebViewLink() except the file name and file id.

Can anyone help me to get the webContentLink or can you figure out the issue about why I cant get webContentLink.

Here is a part of code I tried:

  List<String> linkList=new ArrayList<String>();
  fileFlag=0;
  pageToken = null;
  do {
      driveFiles = service.files().list()
              .setQ("mimeType!='application/vnd.google-apps.folder and "+childFolder.getId()+"' in parents ")
              .setSpaces("drive")
              .setFields("nextPageToken, files(id, name)")
              .setPageToken(pageToken)
              .execute();
      System.out.println("\n\nFile No. : "+driveFiles.getFiles().size());
      for(com.google.api.services.drive.model.File file: driveFiles.getFiles()) {
         linkList.add(file.getWebContentLink());
         System.out.println(file.getName());
         System.out.println("File Link : "+file.getWebContentLink());
         System.out.println(file.getId());
         System.out.println(file.getWebViewLink());
      }
      pageToken = driveFiles.getNextPageToken();
  } while (pageToken != null);

Here is one of the Output

File No. : 4
V2__data.sql
File Link : null
0BzCRyzakN8eJOGRTODhXTFkxUWc
null
V1__table.sql
File Link : null
0BzCRyzakN8eJT09BbG5JUFdNYmc
null
V2__data.sql
File Link : null
0BzCRyzakN8eJa0FyUGw3UFZJZ00
null
V1__table.sql
File Link : null
0BzCRyzakN8eJcDJNWHY4cFNHMW8
null
Kara
  • 6,115
  • 16
  • 50
  • 57
For_A_While
  • 315
  • 2
  • 18
  • I think you have the same issue with [this thread](http://stackoverflow.com/questions/20665881/direct-download-from-google-drive-using-google-drive-api). Check it out, might help. – ReyAnthonyRenacia May 18 '16 at 16:00
  • I have already checked that thread. But webContentLink is not even working for small files. Since that method will be disabled after August 16, I cant use that. The value of webContentLink is null. That is my main issue. @noogui – For_A_While May 18 '16 at 16:03
  • Service Account don't have a web API, so it's possible that this is why there is no webcontentUrl. What are you trying to do? Try looking for a downloadUrl or expoertLinks. – pinoyyid May 18 '16 at 16:06
  • I am looking for downloadUrl. @pinoyyid – For_A_While May 18 '16 at 16:08
  • I've read in this [Google Drive Rest Doc](https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents) that "If you want to allow an unauthorized user to view a file directly in a web browser instead of through the API, use the webContentLink. You can either redirect a user to this URL, or offer it as a clickable link." Try specifying the weblink in the medata data like "webViewLink": string, as stated [here](https://developers.google.com/drive/v3/reference/files#resource-representations). – ReyAnthonyRenacia May 18 '16 at 16:12
  • @noogui That does not apply to Service Accounts. – pinoyyid May 19 '16 at 04:39
  • 1
    @For_A_While you're calling "setFields(files(id, name)", so you're telling drive to only return the id and name properties. Therefore all other properties will be null. – pinoyyid May 19 '16 at 04:42
  • You are right. Thanks for the help. XD.@pinoyyid – For_A_While May 19 '16 at 06:56

1 Answers1

3

As commented by @pinoyyid, adding webContentLink in the setFields property of the drive query solved the problem.

  List<String> linkList=new ArrayList<String>();
  fileFlag=0;
  pageToken = null;
  do {
      driveFiles = service.files().list()
              .setQ("mimeType!='application/vnd.google-apps.folder' and '"+childFolder.getId()+"' in parents ")
              .setSpaces("drive")
              .setFields("nextPageToken, files(id, name, webContentLink)")
              .setPageToken(pageToken)
              .execute();
      System.out.println("\n\nFile No. : "+driveFiles.getFiles().size());
      for(com.google.api.services.drive.model.File file: driveFiles.getFiles()) {
         linkList.add(file.getWebContentLink());
         System.out.println(file.getName());
         System.out.println("File Link : "+file.getWebContentLink());
         System.out.println(file.getId());
         System.out.println(file.getWebViewLink());
      }
      pageToken = driveFiles.getNextPageToken();
  } while (pageToken != null);
For_A_While
  • 315
  • 2
  • 18