7

我正在编写一个应用程序来访问用户在 Google 照片上的图像。

Google Photos 似乎没有“官方”API,但是可以通过Picasa 网络相册 API访问图像。

没有针对 NodeJS/Javascript 的官方 Google Picasa 网络相册 API 参考/文档。

如何从我的 Node 应用程序访问此 API?

4

1 回答 1

1

要从 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错误
  • 每个文件都有许多属性,但在 Api 的 v3 中,默认情况下它不会返回资源的所有字段。您必须通过如下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"'
    • 您可以通过调用获取根文件夹的 id service.files.get({auth:auth, fileId:"root"},callback)- 但您可以简单地使用root放置此 id的位置
    • 列出根文件夹调用中的所有内容service.files.list({auth:auth,q:'parents in "root"'},callback)
    • 如果您有文件的 id,则可以通过service.files.get使用fields:"parents"选项调用来获取文件的文件夹
    • 你有一个文件夹的 id 你可以通过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)
于 2018-01-06T08:47:13.597 回答