5
我正在編寫一個應用程序來訪問Google照片上的用戶圖片。如何從我的節點js應用程序訪問谷歌照片?
似乎Google照片沒有「官方」API,但可以通過Picasa Web Albums API訪問圖像。
NodeJS/Javascript沒有官方的Google Picasa網絡相冊API參考/文檔。
如何從我的Node應用程序訪問此API?
我正在編寫一個應用程序來訪問Google照片上的用戶圖片。如何從我的節點js應用程序訪問谷歌照片?
似乎Google照片沒有「官方」API,但可以通過Picasa Web Albums API訪問圖像。
NodeJS/Javascript沒有官方的Google Picasa網絡相冊API參考/文檔。
如何從我的Node應用程序訪問此API?
要從谷歌照片下載最新的10張照片,請執行quickstart的前兩個步驟,將客戶端密鑰,客戶端ID和重定向插入到下面的咖啡腳本中並運行它。 (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"'
q:'name = "<name of folder>" and mimeType = "application/vnd.google-apps.folder"'
service.files.get({auth:auth, fileId:"root"},callback)
得到的根文件夾的ID - 但你可以簡單地使用root
,你會把這個IDservice.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
的文件相同 - 但您首先需要此文件夾的標識。您可以通過反覆走的路徑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)
看看在[文件](https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol得到這個ID #相片)。 – Robin