我正在编写一个应用来访问Google相册上的用户图片。
似乎没有'官方' Google相册的API,但可以通过Picasa Web Albums API访问图片。
NodeJS / Javascript没有正式的Google Picasa网络相册API参考/文档。
如何从我的Node应用程序访问此API?
答案 0 :(得分:1)
要从google照片下载最新的10张照片,请执行quickstart的前两个步骤,插入客户端密码,客户端ID并重定向到下面的coffeescript并运行它。 (npm install --global coffeescript
然后coffee quickstart.coffee
运行或coffee -c quickstart.coffee
编译为javascript)
我认为用户必须将他们的谷歌照片帐户与谷歌驱动器连接才能实现。
一般情况下使用Reference (v3)和谷歌驱动器的提示:
auth
对象:service.files.list({auth:auth, other params here...},callback)
- 如果您忘记,则会返回Daily Limit for Unauthenticated Use Exceeded
错误fields
选项指定它们,如下所示:service.files.get({auth:auth,fileId:"1y3....",fields:"mimeType, webContentLink, webViewLink, thumbnailLink"},callback)
alt:"media"
放入选项q
选项查询文件。看看available serach parameters。请注意,您可以通过and
,or
和not
组合并嵌套搜索。service.files.list
q:'mimeType = "application/vnd.google-apps.folder"'
来获取所有文件夹的ID
q:'name = "<name of folder>" and mimeType = "application/vnd.google-apps.folder"'
service.files.get({auth:auth, fileId:"root"},callback)
来获取根文件夹的ID - 但您只需使用root
即可将此ID设为service.files.list({auth:auth,q:'parents in "root"'},callback)
service.files.get
选项fields:"parents"
来获取文件的文件夹
service.files.list
调用q:'parents in "0B7..."'
来获取此文件夹的文件(请注意,ID需要"..."
)/one/two/three
的文件夹中的文件与列出文件夹three
的文件相同 - 但您首先需要此文件夹的ID。你可以通过反复走在路上来获得这个id 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)