A Google Apps Script (GAS) library exposing the Google Photos Library API.
Based on https://github.com/tanaikech/GPhotoApp
Currently, the Photos Library API is not available under Advanced Google services. This library enables access via the UrlFetchApp
service.
- Copy + paste the contents of
PhotoApp.js
to a new file in your Apps Script project. - Link the Cloud Platform project to your Google Apps Script project: Apps Script Sidebar > Project Settings > Google Cloud Platform (GCP) Project. See also here.
- Enable the Photos Library API at the GCP Console
- Edit
appsscript.json
in your project to include the scopes required for Google Photos access (see included sample file):
"oauthScopes": [
"https://www.googleapis.com/auth/photoslibrary",
"https://www.googleapis.com/auth/script.external_request"
]
- This library uses modern Javascript. V8 runtime must be enabled.
- Media items can be created only within the albums created by your app (see here). Attempting to upload to an album not created by your app will result in the error:
No permission to add media items to this album
. - Paginated results are returned as iterators. Use
for...of
to iterate over them. If you need all of them, you can useArray.from()
.
Methods | Description |
---|---|
createAlbum(object) | Create new album. |
getAlbumList(excludeNonAppCreatedData) | Get album list. |
getMediaItemList() | Get media item list. |
getMediaItems(object) | Get media items. |
getMediaItem(object) | Gets a media item. |
getMediaItemBlob(object) | Gets data for a media item. |
uploadMediaItems(object) | Upload images to album. |
createAlbum
(albums.create)
function createAlbum() {
const resource = { album: { title: "sample title" } };
const res = PhotoApp.createAlbum(resource);
console.log(res);
}
getAlbumList
(albums.list)
function getAlbumList() {
const res = Array.from(PhotoApp.getAlbumList({excludeNonAppCreatedData: true}));
console.log(res);
}
getMediaItemList
(mediaItems.list)
function getMediaItemList() {
const res = Array.from(PhotoApp.getMediaItemList());
console.log(res);
}
searchMediaItems
(mediaItems.search)
function searchMediaItems() {
const albumId = "###"; // Album ID
const res = Array.from(PhotoApp.searchMediaItems({albumId}));
console.log(res);
}
getMediaItems
(mediaItems.batchGet)
function getMediaItems() {
const resource = { mediaItemIds: ["###", "###"] };
// Note that since the list is limited to requested items, this does not return an iterator.
const res = PhotoApp.getMediaItems(resource);
console.log(res);
}
getMediaItem
(mediaItems.get)
function getMediaItem() {
const id = "###";
const res = PhotoApp.getMediaItem({mediaItemId: id});
console.log(res);
}
function getMediaItems() {
const id = "###";
const mediaItem = PhotoApp.getMediaItem({mediaItemId: id});
const blob = PhotoApp.getMediaItemBlob(mediaItem);
blob.setName(mediaItem.filename);
DriveApp.createFile(blob);
console.log(res);
}
uploadMediaItems
(mediaItems.batchCreate)
function uploadMediaItems() {
const albumId = "###"; // Album ID
const fileId = "###"; // File ID
const url = "###"; // URL of image file
const resource = {
albumId: albumId,
items: [
{
blob: DriveApp.getFileById(fileId).getBlob(),
description: "description1",
filename: "filename1"
},
{
blob: UrlFetchApp.fetch(url).getBlob(),
description: "description2",
filename: "filename2"
}
]
};
const res = PhotoApp.uploadMediaItems(resource);
console.log(JSON.stringify(res));
}
-
v1.1.0 (January 20, 2022) (kwikwag)
- Added some methods
- Refactored code
- Fixed broken pagination API
- Minor breaking interface changes
-
v1.0.0 (February 26, 2020) (tanaikech)
- Initial release.