我正在编写一个应用程序来访问用户在 Google 照片上的图像。
Google Photos 似乎没有“官方”API,但是可以通过Picasa 网络相册 API访问图像。
没有针对 NodeJS/Javascript 的官方 Google Picasa 网络相册 API 参考/文档。
如何从我的 Node 应用程序访问此 API?
我正在编写一个应用程序来访问用户在 Google 照片上的图像。
Google Photos 似乎没有“官方”API,但是可以通过Picasa 网络相册 API访问图像。
没有针对 NodeJS/Javascript 的官方 Google Picasa 网络相册 API 参考/文档。
如何从我的 Node 应用程序访问此 API?
要从 google photos 下载最新的 10 张照片,请执行快速入门的前 2 步,插入客户端密码、客户端 ID 和重定向到下面的咖啡脚本并运行它。(npm install --global coffeescript
然后coffee quickstart.coffee
运行或coffee -c quickstart.coffee
编译为 javascript)
我认为用户必须将他们的谷歌照片帐户与谷歌驱动器连接起来才能工作。
使用参考 (v3)和谷歌驱动器的一般提示:
auth
调用需要身份验证的 api 函数时不要忘记对象: 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
您可以使用该选项查询文件。查看可用的 serach 参数。请注意,您可以通过 和 组合和and
嵌套or
搜索not
。service.files.list
您可以通过使用查询选项调用来获取所有文件夹的 IDq:'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)
- 但您可以简单地使用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。您可以通过迭代地沿着路径走来获取此 IDfs = 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)