Giter VIP home page Giter VIP logo

gphotoapp's Introduction

GPhotoApp

MIT License

Overview

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

Description

In the current stage, Google Photo API is not included in Advanced Google services. But in order to use Google Photo API with Google Apps Script, I created this as a GAS library. So in the current stage, in order to use this library, the following flow is required.

  1. Link Cloud Platform Project to Google Apps Script Project.
  2. Enable Google Photo API at API console
  3. Set the scopes to the manifest file of the Google Apps Script.

When above flow is done, the following functions can be used with this library.

  1. Create new album.
  2. Get album list.
  3. Get media item list.
  4. Get media items.
  5. Upload images to album.

Library's project key

1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9

Methods

In the current stage, I prepared 5 methods that I required.

Methods Description
createAlbum(object) Create new album.
getAlbumList(excludeNonAppCreatedData) Get album list.
getMediaItemList() Get media item list.
getMediaItems(object) Get media items.
uploadMediaItems(object) Upload images to album.

Usage:

1. Linking Cloud Platform Project to Google Apps Script Project:

About this, you can see the detail flow at here.

2. Install library

In order to use this library, please install this library as follows.

  1. Create a GAS project.

    • You can use this library for the GAS project of both the standalone type and the container-bound script type.
  2. Install this library.

    • Library's project key is 1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9.

IMPORTANT

This library uses V8 runtime. So please enable V8 at the script editor.

About scopes

This library use the following 2 scopes.

  • https://www.googleapis.com/auth/photoslibrary
  • https://www.googleapis.com/auth/script.external_request

In this case, when the library is installed, these scopes are also installed.

Methods

createAlbum

Sample script

function createAlbum() {
  var resource = { album: { title: "sample title" } };
  const res = GPhotoApp.createAlbum(resource);
  console.log(res);
}

getAlbumList

Sample script

function getAlbumList() {
  const excludeNonAppCreatedData = true;
  const res = GPhotoApp.getAlbumList(excludeNonAppCreatedData);
  console.log(res);
}

getMediaItemList

Sample script

function getMediaItemList() {
  const res = GPhotoApp.getMediaItemList();
  console.log(res);
}

getMediaItems

Sample script

function getMediaItems() {
  var resource = { mediaItemIds: ["###", "###", , ,] };
  const res = GPhotoApp.getMediaItems(resource);
  console.log(res);
}

uploadMediaItems

Sample script

function uploadMediaItems() {
  const albumId = "###"; // Album ID
  const id = "###"; // file ID
  const url = "###"; // URL of image file
  const resource = {
    albumId: albumId,
    items: [
      {
        blob: DriveApp.getFileById(id).getBlob(),
        description: "description1",
        filename: "filename1"
      },
      {
        blob: UrlFetchApp.fetch(url).getBlob(),
        description: "description2",
        filename: "filename2"
      }
    ]
  };
  const res = GPhotoApp.uploadMediaItems(resource);
  console.log(JSON.stringify(res));
}

IMPORTANT: If the error of No permission to add media items to this album. occurs, please create the album by the script. The official document says as follows.

Media items can be created only within the albums created by your app.

In this case, please create new album by the following script, and please retrieve the album ID.

function createNewAlbum() {
  var options = {
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    payload: JSON.stringify({ album: { title: "sample title" } }),
    contentType: "application/json",
    method: "post"
  };
  var res = UrlFetchApp.fetch(
    "https://photoslibrary.googleapis.com/v1/albums",
    options
  );
  Logger.log(res);
}

Also about this, when the property of isWriteable in the album list is true, the image file can be added to the album. Ref

Licence

MIT

Author

Tanaike

If you have any questions and commissions for me, feel free to tell me.

Update History

  • v1.0.0 (February 26, 2020)

    1. Initial release.

TOP

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.