Friday, October 22, 2021
No menu items!
HomeData Analytics and VisualizationR wrapper for Google Photos API

R wrapper for Google Photos API

This post explains how you can use google photos library API via R programming. You can use this API to create new album and upload pictures to that album and share them with others. You can also fetch information about albums you have access to. Make sure you have access to the following R libraries. If you don’t have access to them you can install them by using install.packages(“packagename”). Once installed you can load library via command library(packagename) library(httr)library(jsonlite)library(dplyr)library(glue)library(googleAuthR)
Google Photos library API requires oauth2.0 authentication of the following two scopes. https://www.googleapis.com/auth/photoslibrary
https://www.googleapis.com/auth/photoslibrary.sharing To fetch oauth2.0 token, you can follow documentation of googleAuthR R package. Once you are done with authentication, you can fetch token using the following command :
k authorization = paste(‘Bearer ‘, k$credentials$access_token)

Lists all albums user has access to

The program below returns all albums with their title and URLs along with number of pictures album has. It also gives information whether album is shareable or not.
getalbum GET(glue(“https://photoslibrary.googleapis.com/v1/albums”),
add_headers(
‘Authorization’ = authorization,
‘Accept’ = ‘application/json’)) %>%
content(., as = “text”, encoding = “UTF-8”) %>%
fromJSON(., flatten = TRUE) %>%
data.frame()

Lists all albums user has access to

The program below returns all albums with their title and URLs along with number of pictures album has. It also gives information whether album is shareable or not.
getalbum GET(glue(“https://photoslibrary.googleapis.com/v1/albums”),
add_headers(
‘Authorization’ = authorization,
‘Accept’ = ‘application/json’)) %>%
content(., as = “text”, encoding = “UTF-8”) %>%
fromJSON(., flatten = TRUE) %>%
data.frame()

Create new album

Here I am creating a new album with title Myblog. You can change it to any name you want.
Album add_headers(
‘Authorization’ = authorization,
‘Content-Type’ = ‘application/json’),
body = list(“album” = list(“title” = “Myblog”)),
encode = “json”) %>%
content(., as = “text”, encoding = “UTF-8”) %>%
fromJSON(., flatten = TRUE) %>%
data.frame()

Share album

In the code below you need to provide albumID in the API https://photoslibrary.googleapis.com/v1/albums/{albumId}:share. I am using albumID from the previous step wherein I created a new album.
request0 add_headers(
‘Authorization’ = authorization,
‘Content-Type’ = ‘application/json’),
body = list(“sharedAlbumOptions” = list(
“isCollaborative” = “true”,
“isCommentable” = “true”)),
encode = “json”)

shareableUrl content(request0, as = “text”, encoding = “UTF-8”) %>%
fromJSON(., flatten = TRUE) %>%
.[[“shareInfo”]] %>%
.[[“shareableUrl”]]

Upload picture

Uploading picture requires two steps – first you need to upload token to a Google Server. Once done you can proceed with uploading image. Here I am using the image myimage.png which is stored in my current working directory.
imageFile request add_headers(
‘Authorization’ = authorization,
‘Content-Type’ = ‘application/octet-stream’,
‘X-Goog-Upload-File-Name’ = imageFile,
‘X-Goog-Upload-Protocol’ = ‘raw’),
body=upload_file(imageFile))

response

Upload picture

Uploading picture requires two steps – first you need to upload token to a Google Server. Once done you can proceed with uploading image. Here I am using the image myimage.png which is stored in my current working directory.
imageFile request add_headers(
‘Authorization’ = authorization,
‘Content-Type’ = ‘application/octet-stream’,
‘X-Goog-Upload-File-Name’ = imageFile,
‘X-Goog-Upload-Protocol’ = ‘raw’),
body=upload_file(imageFile))

response
Previous step alone didn’t upload the picture. You also need to upload token. Refer the program below.
request2 add_headers(
‘Authorization’ = authorization,
‘Content-Type’ = ‘application/json’),
body = list(
“albumId” = Album[1,1],
‘newMediaItems’ = list(
list(“description” = “test upload”,
“simpleMediaItem” = list(“uploadToken” = response)
)
)
),
encode = “json”
)

result %
fromJSON(., flatten = TRUE) %>%
data.frame()

Lists all pictures of an album

If you want to see the URLs of each image of an album, you can use and run the program below. You can increase pageSize parameter to see more than 25 images/videos files.
getmedia POST(glue(“https://photoslibrary.googleapis.com/v1/mediaItems:search”),
add_headers(
‘Authorization’ = authorization,
‘Accept’ = ‘application/json’),
body = list(“albumId” = Album[1,1],
“pageSize” = 25),
encode = “json”
) %>%
content(., as = “text”, encoding = “UTF-8”) %>%
fromJSON(., flatten = TRUE) %>%
data.frame()

Read MoreListenData

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments