English 中文(简体)
Live display new added photos from google drive api
原标题:

Using Google drive Api and a service account I get photos from google drive, all the photos that were originally on the drive are displayed correctly, but if I add a new photo to google drive, the photo itself is not displayed, only its name

const { google } = require( googleapis );
const path = require( path );

const getDriveService = () => {
  const KEYFILEPATH = path.join(__dirname,  settings.json );
  const SCOPES = [ https://www.googleapis.com/auth/drive ];

  const auth = new google.auth.GoogleAuth({
    keyFile: KEYFILEPATH,
    scopes: SCOPES,
  });
  const driveService = google.drive({ version:  v3 , auth });
  return driveService;
};


function getThumbnailUrl(fileId) {
  const timestamp = Date.now(); // Cache-busting parameter
  return `https://drive.google.com/thumbnail?id=${fileId}&timestamp=${timestamp}`;
}
export function getPhotosFromDrive() {
  const driveService = getDriveService();

  return driveService.files.list({
    q: "mimeType= image/jpeg  or mimeType= image/png ", // Filter for JPEG and PNG images
    fields:  files(id, name, webViewLink) , // Specify the fields to retrieve
  })
    .then(response => {
      const photos = response.data.files;
      return photos.map(photo => ({
        name: photo.name,
        id: photo.id,
        webViewLink: getThumbnailUrl(photo.id)
      }));
    })
    .catch(error => {
      console.error( Error retrieving photos: , error);
      throw error;
    });
}
import { getPhotosFromDrive } from  backend/photosGallery ;

$w.onReady(function () {
  getPhotosFromDrive()
    .then(photos => {
      const galleryItems = photos.map(photo => ({
        type:  image ,
        src: photo.webViewLink,
        title: photo.name
      }));

      $w( #gallery1 ).items = galleryItems;
    })
    .catch(error => {
      console.error( Error retrieving photos: , error);
      // Handle the error
    });
});

enter image description here

问题回答

Suggestion

Please note that we do not have access to the specific details of your setup, so the community member can only make assumptions to assist in investigating the issue. The following answer can serve as a guideline for further troubleshooting:

The problem you re experiencing could have several potential causes. To narrow down the issue, you should consider the following factors:

  1. What do you see when you directly access the link of the newly added image?
  2. Compare the properties of the new image that is not displaying (e.g., sharing permissions, file size) with at least one image that is successfully displayed in your Google Drive.

Based on my own testing:

  • The newly added image from my drive displays a broken image icon:

    Broken image icon

  • When I opened the link of the broken image, which was a test jpeg image pulled by the service account, I encountered the following:

    Broken image

  • Fortunately, I was able to resolve the issue:

    Fixed image

To fix the issue, I followed these steps:

  1. Ensured that the service account has at least the "View" or "Read" permissions for the new file.
  2. Confirmed that the new image has "Anyone with the link" general access permission in Google Drive, allowing it to be publicly accessible and visible on my test HTML page.

Again, I may have a different setup from your actual environment. But you could start further investigating your issue by trying out what I have done to narrow down the problem.

References





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签