My aim is to create a local copy of my photos in Google Photos to be able to use them in my Quarto Website. I choose to use R for this.
Challenges
This is the kind of thing that works great when it works, but that can enter an infinite loop of trial-and-error if you miss a tiny, vital detail.
Sometimes the procedure has been described in detail in older posts, but specific configurations or methods have changed since. So it is important to understand what is needed and to adapt the steps according to the most recent documentation.
Make sure to update the .Renviron file, then you can (re-)load it in the current R session with:
readRenviron("~/.Renviron")
Read credentials and authenticate
The credentials are in a json file in a private folder, the environment variable contains this location. Now we can check if the file exists, and read it:
In an interactive session, this will open a tab/window in the browser to complete authentication and confirm permissions for the app. It might use information in the cache, if available.
If this is run non-interactively, it will try to use the information in the cache, but will fail if this info is stale.
Final steps of authentication:
k <-token_fetch(token=tkn)authorization =paste('Bearer', k$credentials$access_token)
Album information
Now we can get the album information using function GET:
getalbum <-GET("https://photoslibrary.googleapis.com/v1/albums",add_headers('Authorization'= authorization,'Accept'='application/json'),query =list("pageSize"=50)) |>content( as ="text", encoding ="UTF-8") |>fromJSON()
We downloaded the information for all fotos. The baseUrl links are useful during the R session, but are not good for sharing the links to the photos. They are random urls and become defunct after the session is closed.
For example, this will display the image using the baseUrl when rendering this page, but will eventually disappear:
One way to share the photos is by selecting existing files, creating shareable albums with the API and downloading the shareableURL of the album and photos. I still haven’t worked out the code for doing that in R.
Another option is to just download the photos in the size needed for the session/website and share the productUrl to link back to the Google photos page for the image.
For example we can visualise one photo with the image_read function in the magick library using the baseUrl attribute: