Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get thumbnail using Google Drive API

My application is using an OAuth2 service account to copy a file from a user's Google Drive. I'm using the Google Drive client api via Java to obtain a Drive object with the requested scope being "https://www.googleapis.com/auth/drive". I'm able to make a copy of the Google Docs document, but the thumbnailLink is not retrievable. I'm getting a 403 Forbidden error. I'm quite confident this is a bug on the Google side. If I put a breakpoint in my code on he line that gets the 403 Forbidden result, I can (when logged in as the user whose Google Drive I'm copying from) use the thumbnailLink to get the thumbnail in my browser.

Here's a rewritten snippet of the code I'm using, where sourceFile is the com.google.api.services.drive.model.File that is being copied from and sourceDrive is the com.google.api.services.drive.Drive object that I mentioned above:

File newFile = new File();
newFile.setTitle( sourceFile.getTitle() );
newFile.setDescription( sourceFile.getDescription() );
newFile.setParents( sourceFile.getParents() );
File copiedFile = sourceDrive.files().copy( sourceFile.getId(), newFile ).execute();
String thumbnailLink = copiedFile.getThumbnailLink();
HttpRequest request = sourceDrive.getRequestFactory().buildGetRequest( new GenericUrl( thumbnailLink ) );
HttpResponse response = request.execute();

As mentioned above, the request.execute() line produces an exception due to the 403 Forbidden error being returned. If I put a breakpoint on the last line of the code snippet above, I am able to take the thumbnailLink and paste it into my browser that is logged in as the user whose Drive is being copied from and it gets the thumbnail returned successfully.

like image 849
cwjos Avatar asked May 08 '14 17:05

cwjos


3 Answers

for the thumbnail link you can create with

https://drive.google.com/thumbnail?id={YOUR_IMAGE_FILE_ID}

By default that will return thumbnail of 220 px(max-width-220px max-height-220px, keeping the aspect ratio)

there are more parameters you can send with the link such as width, height Then we need to use 'sz' querystring https://drive.google.com/thumbnail?sz=w100-h100&id={YOUR_IMAGE_FILE_ID}

here sz=w100-h100 means height 100px and width 100px. you can pass anyone of them too. If you are sending both height and width then have to be sure about those values whether it is maintaining the original aspect ratio of the image.

like image 89
isambitd Avatar answered Oct 19 '22 02:10

isambitd


I've posted this answer in a related question but it should be applicable here as well. We've recently made a change to fix this issue. Please try your code again and see if you are still getting this error. I was able to run the following code to successfully download a thumbnail of my Google Document.

# Get oauth credentials
...
# Authorize an http object
http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

# Google Document type ID
docId = '1ns9x5BMIZAeUR-eXerqgpaHBBGkl_-_KCVpVoV5opn8'
files = drive_service.files().get(fileId=docId).execute()

thumbnailLink = files['thumbnailLink']
print 'Downloading thumbnail at: ', thumbnailLink
response, content = http.request(thumbnailLink)
print response.status

with open('thumbnail.jpg', 'wb') as f:
  f.write(content)
like image 34
Kalyan Reddy Avatar answered Oct 19 '22 02:10

Kalyan Reddy


Update: Update to the discussion of the request fields.

As far as I can tell, there is literally no good documentation on the solution for this. However, with the help of a co-worker and the REST API operation, I've gotten the solution.

To get valid thumbnailLink values using the Drive Java Client Library, you have to modify your query. Since you're doing a custom field request you have to be sure to add the other fields you want as well. Here is how I've gotten it to work:

Drive.Files.List request = yourGoogleDriveService.files()
                    .list()
                    .setFields("files/thumbnailLink, files/name, files/mimeType, files/id")
                    .setQ("Your file param and/or mime query");

FileList files = request.execute();
files.getFiles();  //Each File in the collection will have a valid thumbnailLink

Hope this helps!

like image 1
creativedrewy Avatar answered Oct 19 '22 03:10

creativedrewy