Vad
Vad

Reputation: 53

Why Google Drive getExportLinks returns null?

I'm uploading a Word document:

    com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
    body.setTitle(file.getName());
    body.setMimeType(mimeType);

    FileContent mediaContent = new FileContent(mimeType, file);

    com.google.api.services.drive.model.File uploadedFile = service.files()
            .insert(body, mediaContent).execute();

    String downloadUrl = file.getExportLinks().get(mimeType);

The upload goes through fine. However, call to file.getExportLinks() returns null, and, thus, this line a null pointer. Does anybody know why getExportLinks() returns null?

Upvotes: 5

Views: 982

Answers (2)

Roman
Roman

Reputation: 2549

You need to set the correct Scope to access the links.

https://developers.google.com/drive/web/scopes

Upvotes: 0

eddyparkinson
eddyparkinson

Reputation: 3700

Grab the document key and create the link like this:

Drive driveService = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential).build();
File file = driveService.files().get(this.spreadsheetKey).execute();
String downloadUrl = file.getExportLinks().get("application/pdf");

Example downloadUrl: https://docs.google.com/feeds/download/spreadsheets/Export?key=somekey&exportFormat=pdf

Note: you can paste the above url into a browser, and it will download as long as you are logged into google drive.

No idea if you can create the link without the document key.

Upvotes: 1

Related Questions