I am writing an app to access a user's images on Google Photos.
It seems that there is no 'official' API for Google Photos, however the images can be accessed via the Picasa Web Albums API.
There are no official Google Picasa Web Albums API references/documentation for NodeJS / Javascript.
How can I access this API from my Node app ?
To download the latest 10 photos from google photos, do the first 2 steps of the quickstart, insert the client secret, client id and redirect into the coffeescript below and run it. (npm install --global coffeescript
then coffee quickstart.coffee
to run or coffee -c quickstart.coffee
to compile to javascript)
I think the user has to connect their google photos account with google drive for this to work.
Hints for working with the Reference (v3) and google drive in general:
auth
object when you call api functions that require authentication: service.files.list({auth:auth, other params here...},callback)
- if you forget, it returns a Daily Limit for Unauthenticated Use Exceeded
errorfields
option like this: service.files.get({auth:auth,fileId:"1y3....",fields:"mimeType, webContentLink, webViewLink, thumbnailLink"},callback)
alt:"media"
in the optionsq
option. Look at the available serach parameters. Note that you can combine and nest searches via and
, or
and not
.service.files.list
with the query option q:'mimeType = "application/vnd.google-apps.folder"'
q:'name = "<name of folder>" and mimeType = "application/vnd.google-apps.folder"'
service.files.get({auth:auth, fileId:"root"},callback)
- but you can simply use root
where you would put this idservice.files.list({auth:auth,q:'parents in "root"'},callback)
service.files.get
with the fields:"parents"
optionservice.files.list
with a query option q:'parents in "0B7..."'
(note that the id needs "..."
)/one/two/three
is the same as listing the files of the folder three
- but you'll need the id of this folder first. you can get this id by iteratively walking down the pathfs = require('fs')
readline = require('readline')
google = require('googleapis')
googleAuth = require('google-auth-library')
SCOPES = [ 'https://www.googleapis.com/auth/drive' ] # scope for everything :D
TOKEN_PATH = './token.json'
CLIENT_SECRET = <your client secret here>
CLIENT_ID = <your client id here>
REDIRECT = <your redirect url here>
authorize = (callback) ->
auth = new googleAuth
oauth2Client = new auth.OAuth2(CLIENT_ID, CLIENT_SECRET,REDIRECT)
# Read the Token at ./token.json or get a new one
fs.readFile TOKEN_PATH, (err, token) ->
if err
getNewToken oauth2Client, callback
else
oauth2Client.credentials = JSON.parse(token)
callback oauth2Client
getNewToken = (oauth2Client, callback) ->
authUrl = oauth2Client.generateAuthUrl({access_type: 'offline', scope: SCOPES})
console.log 'Authorize this app by visiting this url: ', authUrl
rl = readline.createInterface({input: process.stdin,output: process.stdout})
rl.question 'Enter the code in the address bar without the "#"(?code=<code>#)', (code) ->
rl.close()
oauth2Client.getToken code, (err, token) ->
oauth2Client.credentials = token
fs.writeFile TOKEN_PATH, JSON.stringify(token) # store token for later
callback oauth2Client
authorize (auth)->
service = google.drive('v3')
# get ids of the 10 most recent photos
# every request needs the auth:auth
service.files.list {auth:auth,pageSize: 10,orderBy: 'createdTime desc',q:"mimeType = 'image/jpeg'"},(err,response)->
for file in response.files
dest = fs.createWriteStream(file.name)
# you have to add the alt:"media" option to get the file contents
# if you want a link to the file that can be used in an <img src=''> tag: add fields:"webContentLink"
service.files.get({auth:auth,fileId:file.id,alt:"media"}).pipe(dest)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With