shared album using google photos API for server to server

107 views Asked by At

i'm trying to build simple service to retrieve photos or access a shared album using google phots API , node.js and service account credentials. this is the code i'm using -

import { google } from 'googleapis';

const SCOPES = ['https://www.googleapis.com/auth/photoslibrary.readonly'];

// Load the service account key
import key from './servicekeyoauth.json' assert { type: 'json' };

// Create an OAuth2 client
const authClient = new google.auth.JWT({
    email: key.client_email,
    key: key.private_key,
    scopes: SCOPES,
});

// Initialize the Photos Library API client
const photosLibrary = google.photoslibrary({
    version: 'v1',
    auth: authClient,
});

async function getPhotosFromSharedAlbum() {
    try {
        // Authenticate with the OAuth2 client
        await authClient.authorize();

        // Replace 'sharedAlbumId' with your shared album ID
        const sharedAlbumId = 'ALBUM_ID';

        // List the media items (photos) from the shared album
        const response = await photosLibrary.mediaItems.search({
            albumId: sharedAlbumId,
        });

        const mediaItems = response.data.mediaItems;

        // Process the media items (photos)
        console.log('Media Items:');
        mediaItems.forEach((mediaItem, index) => {
            console.log(`Photo ${index + 1}:`);
            console.log(`Title: ${mediaItem.filename}`);
            console.log(`Media URL: ${mediaItem.baseUrl}`);
        });
    } catch (error) {
        console.error('Error:', error.message);
    }
}

// Call the function to retrieve photos from the shared album
getPhotosFromSharedAlbum();

i'm getting the error - google.photoslibrary is not a function

i changed the code to - new google.photoslibrary({ but got this error - google.photoslibrary is not a constructor

any idea what can be the issue?

0

There are 0 answers