phe*_*elm 6 javascript picasa google-data-api node.js google-photos
我正在编写一个应用程序来访问Google相册上的用户图片.
似乎Google照片没有"官方"API,但可以通过Picasa网络相册API访问这些图片.
NodeJS/Javascript没有正式的Google Picasa网络相册API参考/文档.
如何从我的Node应用程序访问此API?
要从 google photos 下载最新的 10 张照片,请执行快速入门的前 2 个步骤,插入客户端密钥、客户端 ID 并重定向到下面的 Coffeescript 中并运行它。(npm install --global coffeescript然后coffee quickstart.coffee运行或coffee -c quickstart.coffee编译为javascript)
我认为用户必须将他们的谷歌照片帐户与谷歌驱动器连接起来才能正常工作。
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。查看可用的搜索参数。请注意,您可以通过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 的位置来获取根文件夹的 idservice.files.list({auth:auth,q:'parents in "root"'},callback)service.files.get如果您有文件的 ID,则可以通过使用选项fields:"parents"调用来获取文件的文件夹service.files.list你有一个文件夹的 id,你可以通过使用查询选项调用来获取该文件夹的文件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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3100 次 |
| 最近记录: |