R wrapper for Google Photos API

Deepanshu Bhalla Add Comment ,
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 <- gargle::token_fetch(token = gar_token())
authorization = paste('Bearer ', k$credentials$access_token)

R wrapper for Google Photos API
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 <- POST("https://photoslibrary.googleapis.com/v1/albums",
              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 <- POST(glue('https://photoslibrary.googleapis.com/v1/albums/{Album[1,1]}:share'),
                 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 <- "myimage.png"
request <- POST("https://photoslibrary.googleapis.com/v1/uploads",
                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 <- content(request, as = "text", encoding = "UTF-8")

Previous step alone didn't upload the picture. You also need to upload token. Refer the program below.

request2 <- POST("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate",
                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 <- content(request2, as = "text", encoding = "UTF-8") %>% 
    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()

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

0 Response to "R wrapper for Google Photos API"
Next → ← Prev