How to access google photos from my node js app?

I am writing an application to access user images in Google Photos.

There seems to be no “official” Google Photos API, however images can be accessed through the Picasa Web Albums API .

NodeJS / Javascript has no official links / documentation for the Google Picasa Web Albums API for NodeJS / Javascript.

How can I access this API from my Node application?

+5
source share
1 answer

To download the last 10 photos from Google photos, do the first 2 steps of quickstart , insert the client secret, client id and redirect to coffeescript below and run it. ( npm install --global coffeescript then coffee quickstart.coffee to run or coffee -c quickstart.coffee to compile in javascript)

I think the user should connect their google google account using google drive for this.

Help (v3) and Google’s shared drive tips:

  • do not forget the auth object when calling api functions requiring authentication: service.files.list({auth:auth, other params here...},callback) - if you forget, it returns a Daily Limit for Unauthenticated Use Exceeded error
  • Each file has many properties, but in v3 Api it does not return all resource fields by default. You must specify them using the fields parameter as follows: service.files.get({auth:auth,fileId:"1y3....",fields:"mimeType, webContentLink, webViewLink, thumbnailLink"},callback)
  • if you want to download the file, put alt:"media" in the options
  • you can request files with the q option. Check out serach options available . Note that you can combine and embed a search through and , or and not .
  • there are no real "folders" on the Google drive. each file can have several "parents".
    • you can get the identifiers of all folders by calling service.files.list with the query option q:'mimeType = "application/vnd.google-apps.folder"'
    • to get a folder by name, use the query q:'name = "<name of folder>" and mimeType = "application/vnd.google-apps.folder"'
    • you can get the root folder identifier by calling service.files.get({auth:auth, fileId:"root"},callback) - but you can just use root where you would put that id
    • to list all things in a call to the root folder service.files.list({auth:auth,q:'parents in "root"'},callback)
    • If you have a file identifier, you can get the file folder by calling service.files.get with the fields:"parents" option
    • you have a folder identifier, you can get the files of this folder by calling service.files.list with the query option q:'parents in "0B7..."' (note that id needs "..." )
    • listing files in a folder with the tag /one/two/three same as listing files in a three folder - but first you will need the identifier for that folder. you can get this identifier by iteratively walking along the path.
 fs = 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) 
0
source

All Articles