Code Monkey home page Code Monkey logo

gphotoapp's Introduction

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"
  ]

Public library

I got a contact that the library key had been removed. The script ID of the library is as follows.

1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9
  • How to install the Google Apps Script can be seen at here.

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.0.0 (February 26, 2020) (tanaikech)

    1. Initial release.
  • v1.1.0 (January 20, 2022) (kwikwag)

    1. Added some methods
    2. Refactored code
    3. Fixed broken pagination API
    4. Minor breaking interface changes

gphotoapp's People

Contributors

kwikwag avatar tanaikech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

gphotoapp's Issues

Wrong Query Parameter

Referring to

params.url = url + (pageToken ? `&nextPageToken=${pageToken}` : "");

As Is: nextPageToken is used as a query parameter, this leads to an error.

Should be: pageToken is the correct query parameter. Please see docs as reference: https://developers.google.com/photos/library/reference/rest/v1/albums/list

Note: I only tested GetAlbumList, but I assume the same error occurs for GetMediaItemList, see https://github.com/tanaikech/GPhotoApp/blob/master/GPhotoApp.js#L131

By the way: Nice library. Very useful. Thank you ๐Ÿ‘

Sharing

If like me, anyone needs to include sharing you will

  1. add "https://www.googleapis.com/auth/photoslibrary.sharing", to your oauthScopes in appsscript.json

  2. add the following method to PhotoApp.gs, and call the method with the ID you get back from creating an album. Please note, you can only initiate sharing on an album your app has created.

 this.shareAlbum = (id) => {
    return _apiCall({
      path: "albums/"+id+":share",
      payload: {
        sharedAlbumOptions : {
          isCollaborative : true,
        }
      }
    })
  }

If you'd like to improve this to dynamically add isCommentable as well to the sharedAlbumOptions object feel free, but this met my needs.

Cheers

GetMediaItemList is not a function

function myFunction() {
  var res = Array.from(GPhotoApp.GetMediaItemList());
  console.log(res);
}

Result:
TypeError: GPhotoApp.GetMediaItemList is not a function

Add Function Search Items By AlbumId

If you want to get all media items of the album. You can use this function.
Thank you for helpful library.
SearchMediaItemList(id_album) { var mediaItems, pageToken, params, res, url; url = "https://photoslibrary.googleapis.com/v1/mediaItems:search"; var payload = { 'albumId': id_album }; params = { method: 'post', muteHttpExceptions: true, headers: { Authorization: Bearer ${this.accessToken} }, payload : payload }; mediaItems = []; pageToken = ""; params.url = url; while (true) { payload.pageToken = pageToken ? pageToken : ''; params.payload = payload; res = UrlFetchApp.fetchAll([params])[0]; r = JSON.parse(res.getContentText()); if (res.getResponseCode() !== 200) { throw new Error(res.getContentText()); } Array.prototype.push.apply(mediaItems, r.mediaItems); pageToken = r.nextPageToken; if (!pageToken) { break; } } return mediaItems; }

Full code at: https://pastebin.com/qg1fQsfW

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.