2015-07-03 35 views
5

我正在編寫一個應用程序來訪問Google照片上的用戶圖片。如何從我的節點js應用程序訪問谷歌照片?

似乎Google照片沒有「官方」API,但可以通過Picasa Web Albums API訪問圖像。

NodeJS/Javascript沒有官方的Google Picasa網絡相冊API參考/文檔。

如何從我的Node應用程序訪問此API?

+0

看看在[文件](https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol得到這個ID #相片)。 – Robin

回答

0

要從谷歌照片下載最新的10張照片,請執行quickstart的前兩個步驟,將客戶端密鑰,客戶端ID和重定向插入到下面的咖啡腳本中並運行它。 (npm install --global coffeescript然後coffee quickstart.coffee運行或coffee -c quickstart.coffee編譯爲javascript)

我認爲用戶必須連接他們的谷歌照片帳戶與谷歌驅動器爲此工作。

提示對於一般與Reference (v3)和谷歌驅動作用:

  • 沒有,當你調用需要驗證的API函數忘記auth對象:service.files.list({auth:auth, other params here...},callback) - 如果你忘了,它返回一個錯誤Daily Limit for Unauthenticated Use Exceeded
  • 每個文件都有很多屬性,但在Api的v3中,默認情況下它不會返回資源的所有字段。你必須通過這樣的fields選項來指定它們:service.files.get({auth:auth,fileId:"1y3....",fields:"mimeType, webContentLink, webViewLink, thumbnailLink"},callback)如果你想下載一個文件
  • ,把alt:"media"
  • 您可以用q選項查詢文件的選項。看看available serach parameters。請注意,您可以通過andornot合併和嵌套搜索。
  • 谷歌驅動器中沒有真正的「文件夾」。每個文件可以有多個「父母」。
    • 您可以通過調用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)
    • ,如果你有一個文件的ID,您可以通過調用獲取該文件的文件夾(S) service.files.getfields:"parents"選項
    • 你,你有一個文件夾的id,你可以通過調用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) 
相關問題