Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Why can't I get a thumbnail of a pdf file with Google Drive API?

Hi there,

I would like to upload a pdf file with Google Drive API and get its thumbnail.
A few weeks ago this was possible, but yesterday I noticed that I can no longer get the thumbnail link.
Has the specification of the Google Drive API changed or is there something I need to be aware of when using this API?

I can confirm that even if I upload a pdf file with the sample code below and get the file information, I cannot get a Thumbnail link.

upload_file.go

 
Show More
package main
 
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
 
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
)
 
func main() {
ctx := context.Background()
 
b, err := ioutil.ReadFile("secret.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
 
config, err := google.JWTConfigFromJSON(b, drive.DriveScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := config.Client(ctx)
 
srv, err := drive.New(client)
if err != nil {
log.Fatalf("Unable to retrieve Drive client: %v", err)
}
 
fileName := "sample.pdf"
file, err := os.Open(fileName)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer file.Close()
 
driveFile := &drive.File{Name: fileName}
 
res, err := srv.Files.Create(driveFile).Media(file).Fields("id, thumbnailLink").Do()
if err != nil {
log.Fatalf("Could not create file: %v", err)
}
 
fmt.Printf("File ID: %s\n", res.Id)
fmt.Printf("File Content Link: %s\n", res.WebContentLink)
fmt.Printf("File Web Link: %s\n", res.WebViewLink)
fmt.Printf("Thumbnail Link: %s\n", res.ThumbnailLink)
}
 

get_file_info.go

Show More
package main
 
import (
"context"
"fmt"
"io/ioutil"
"log"
 
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
)
 
func main() {
ctx := context.Background()
 
b, err := ioutil.ReadFile("secret.json")
if err != nil {
log.Fatalf("Unable to read service account key file: %v", err)
}
 
config, err := google.JWTConfigFromJSON(b, drive.DriveScope)
if err != nil {
log.Fatalf("Unable to parse service account key file to config: %v", err)
}
client := config.Client(ctx)
 
srv, err := drive.New(client)
if err != nil {
log.Fatalf("Unable to retrieve Drive Client: %v", err)
}
 
fileId := "xxxxxxxxxxxxxxxxxxxxx"
 
file, err := srv.Files.Get(fileId).Fields("id, name, mimeType, size, createdTime, thumbnailLink, webContentLink, webViewLink").Do()
if err != nil {
log.Fatalf("Unable to retrieve file: %v", err)
}
 
fmt.Printf("File ID: %s\n", file.Id)
fmt.Printf("Name: %s\n", file.Name)
fmt.Printf("MIME Type: %s\n", file.MimeType)
fmt.Printf("Size: %s bytes\n", file.Size)
fmt.Printf("Created Time: %s\n", file.CreatedTime)
fmt.Printf("File Content Link: %s\n", file.WebContentLink)
fmt.Printf("File Web Link: %s\n", file.WebViewLink)
fmt.Printf("Thumbnail Link: %s\n", file.ThumbnailLink)
}

 Thank you in advance.

1 0 415
0 REPLIES 0
Top Labels in this Space
Top Solution Authors