This is a GAS library for retrieving and creating the albums and media items using Google Photo API using Google Apps Script (GAS).
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.
- Link Cloud Platform Project to Google Apps Script Project.
- Enable Google Photo API at API console
- 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.
- Create new album.
- Get album list.
- Get media item list.
- Get media items.
- Upload images to album.
1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9
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. |
About this, you can see the detail flow at here.
In order to use this library, please install this library as follows.
-
Create a GAS project.
- You can use this library for the GAS project of both the standalone type and the container-bound script type.
-
- Library's project key is
1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9
.
- Library's project key is
This library uses V8 runtime. So please enable V8 at the script editor.
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.
function createAlbum() {
var resource = { album: { title: "sample title" } };
const res = GPhotoApp.createAlbum(resource);
console.log(res);
}
function getAlbumList() {
const excludeNonAppCreatedData = true;
const res = GPhotoApp.getAlbumList(excludeNonAppCreatedData);
console.log(res);
}
function getMediaItemList() {
const res = GPhotoApp.getMediaItemList();
console.log(res);
}
function getMediaItems() {
var resource = { mediaItemIds: ["###", "###", , ,] };
const res = GPhotoApp.getMediaItems(resource);
console.log(res);
}
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 istrue
, the image file can be added to the album. Ref
If you have any questions and commissions for me, feel free to tell me.
-
v1.0.0 (February 26, 2020)
- Initial release.