Skip to content

This is a GAS library for retrieving and creating the albums and media items using Google Photo API using Google Apps Script (GAS).

License

Notifications You must be signed in to change notification settings

kwikwag/GPhotoApp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GPhotoApp

MIT License

Overview

A Google Apps Script (GAS) library exposing the Google Photos Library API.

Based on https://github.com/tanaikech/GPhotoApp

Description

Currently, the Photos Library API is not available under Advanced Google services. This library enables access via the UrlFetchApp service.

Usage

  1. Copy + paste the contents of PhotoApp.js to a new file in your Apps Script project.
  2. Link the Cloud Platform project to your Google Apps Script project: Apps Script Sidebar > Project Settings > Google Cloud Platform (GCP) Project. See also here.
  3. Enable the Photos Library API at the GCP Console
  4. 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"
  ]

Notes

  • 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 use Array.from().

Documentation

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.

Sample scripts

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);
}

getMediaItemBlob

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));
}

License

MIT

Authors

Tanaike, kwikwag

Update History

  • v1.1.0 (January 20, 2022) (kwikwag)

    1. Added some methods
    2. Refactored code
    3. Fixed broken pagination API
    4. Minor breaking interface changes
  • v1.0.0 (February 26, 2020) (tanaikech)

    1. Initial release.

About

This is a GAS library for retrieving and creating the albums and media items using Google Photo API using Google Apps Script (GAS).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%