首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用元数据API迭代Google Drive文件

使用元数据API迭代Google Drive文件
EN

Stack Overflow用户
提问于 2017-01-13 13:46:10
回答 2查看 410关注 0票数 0

我想要显示驱动器文件元数据

"iconLink","thumbnailLink“

驱动器中的每个文件,但仅获取kind,id,name,mimeType等字段的输出。而其他字段则不显示。

代码语言:javascript
运行
AI代码解释
复制
function loadDriveApi() {
    gapi.client.load('drive', 'v3', listFiles);
}

function listFiles() {
    var request = gapi.client.drive.files.list({});

    request.execute(function(resp) {
        var files = resp.files;
        if (files && files.length > 0) {
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                appendPre(file.iconLink);
            }
        } else {
            appendPre('No files found.');
        }
    });
}
EN

回答 2

Stack Overflow用户

发布于 2017-01-13 16:38:27

至少需要作用域:https://www.googleapis.com/auth/drive.metadata.readonly + OAuth (API-Key & Client-ID)

你可以在https://developers.google.com/drive/v3/reference/files/list上测试它。

在"fields“输入中添加:文件(iconLink,thumbnailLink)

如果您使用https://apis.google.com/js/api.js,请务必将您的域添加到API-Key -> HTTP-Referrer & Client-ID -> JavaScript-Source & Forwarding-URI (@ https://console.developers.google.com/apis/credentials)

你可以在这里找到一个基本的gapi用法示例:https://github.com/google/google-api-javascript-client/blob/51aa25bed4f6c36d8e76fd3b9f7e280ded945c98/samples/loadedDiscovery.html

一段时间以前,我简化了gapi客户端,因为我不喜欢在方法中混合使用回调和thenable。这对我来说很有效(假设api.js已经加载),但在响应中只有100个文件条目。

代码语言:javascript
运行
AI代码解释
复制
window.gapiPromisified = {
  apiKey: 'XXXXXXXXXXX',
  clientId: 'XXXXX-XXXXXX.apps.googleusercontent.com'
}

window.gapiPromisified.init = function init () {
  return new Promise(resolve => {
    gapi.load('client:auth2', () => {
      if (!document.getElementById('gapiAuthButton')) {
        let authButton = document.createElement('button')
        authButton.id = 'gapiAuthButton'
        authButton.style.display = 'none'
        authButton.style.marginLeft = 'auto'
        authButton.style.marginRight = 0
        document.body.insertBefore(authButton, document.body.firstChild)
        authButton.addEventListener('click', () => {
          let GoogleAuth = gapi.auth2.getAuthInstance()
          if (GoogleAuth.isSignedIn.get()) {
            GoogleAuth.signOut()
          } else {
            GoogleAuth.signIn()
          }
        })
      }
      gapi.client.setApiKey(this.apiKey)
      gapi.auth2.init({ client_id: this.clientId })
      .then(() => resolve())
    })
  })
}

window.gapiPromisified.signIn = function signIn () {
  return new Promise(resolve => {
    let GoogleAuth = gapi.auth2.getAuthInstance()
    // Listen for sign-in state changes
    GoogleAuth.isSignedIn.listen(isSignedIn => {
      let authButton = document.getElementById('gapiAuthButton')
      if (isSignedIn) {
        authButton.textContent = 'Sign-out'
        resolve()
      } else {
        authButton.textContent = 'Sign-in'
      }
    })
    // Handle the initial sign-in state
    let authButton = document.getElementById('gapiAuthButton')
    let isSignedIn = GoogleAuth.isSignedIn.get()
    authButton.textContent = (isSignedIn) ? 'Sign-out' : 'Sign-in'
    document.getElementById('gapiAuthButton').style.display = 'block'
    if (isSignedIn) {
      resolve()
    } else {
      GoogleAuth.signIn()
    }
  })
}

window.gapiPromisified.getReady = function getReady () {
  if (!gapi.hasOwnProperty('auth2')) {
    return this.init()
    .then(() => this.signIn())
  } else {
    if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
      return Promise.resolve()
    } else {
      return this.signIn()
    }
  }
}

window.gapiPromisified.getScopes = function getScopes (scopes) {
  return new Promise((resolve, reject) => {
    let GoogleUser = gapi.auth2.getAuthInstance().currentUser.get()
    if (GoogleUser.hasGrantedScopes(scopes)) {
      resolve()
    } else {
      // method returns goog.Thenable
      GoogleUser.grant({ 'scope': scopes })
      .then(onFulfilled => {
        resolve(onFulfilled)
      }, onRejected => {
        reject(onRejected)
      })
    }
  })
}

window.gapiPromisified.loadAPI = function loadAPI (urlOrObject) {
  return new Promise((resolve, reject) => {
    // method returns goog.Thenable
    gapi.client.load(urlOrObject)
    .then(onFulfilled => {
      resolve(onFulfilled)
    }, onRejected => {
      reject(onRejected)
    })
  })
}

window.gapiPromisified.metadata = function metadata () {
  return new Promise((resolve, reject) => {
    this.getReady()
    .then(() => this.getScopes('https://www.googleapis.com/auth/drive.metadata.readonly'))
    .then(() => this.loadAPI('https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'))
    .then(() => {
      gapi.client.drive.files.list({
        fields: 'files(iconLink,thumbnailLink)'
      })
      .then(onFulfilled => {
        resolve(onFulfilled)
      }, onRejected => {
        reject(onRejected)
      })
    })
  })
}

window.gapiPromisified.metadata()
.then(res => {
  res.result.files.forEach(file => {
    console.log(file.iconLink)
    console.log(file.thumbnailLink)
  })
})
票数 1
EN

Stack Overflow用户

发布于 2017-01-13 22:03:32

在v3中,您需要指定希望在元数据响应中包含哪些字段。请参见fields=参数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41636049

复制
相关文章
使用wget命令下载Google drive上的文件
此处的文件是指公开的文件,不需要输入密码也不需要登录Google drive即可获取的文件。
烤粽子
2021/07/07
9.2K0
Google drive copy File
Creates a copy of a file and applies any requested updates with patch semantics. Try it now.
拿我格子衫来
2022/01/24
4560
用 Cyberduck下载 / 上传 Google Drive 大文件
用Google Colab训练模型时,可以将训练好的权重模型存储到Google Drive上,很方便。但是,将模型权重下载下来就特别麻烦。另外,上传bert模型和数据集也特别困难。尝试了一些方法,最好的方法是使用Cyberduck(就是下面这个鸭子),传输稳定,并且可以断点续传。
SeanCheney
2020/05/09
4.2K0
用 Cyberduck下载 / 上传 Google Drive 大文件
rclone挂载Google Drive
元旦在土区成功购买Google one 2T之后,就着手开始用rclone挂载Google Drive
行 者
2023/10/20
8870
如何使用命令行将数据上传到Google Drive?
要想命令行将数据上传到Google Drive,这里推荐一个工具,叫 rclone。
marsggbo
2022/05/11
1.1K0
Google Drive 的信息检索
对于使用 Google 全家桶的公司,Google 文档类的信息泄露时常发生。出现这种情况主要的原因是文档的权限设置问题,用户可能将文档配置为 anyoneCanFind, anyoneWithLink, domainCanFind, domainWithLink,这四种权限都属于比较公开的权限。后两个属于在域内可以查看到文档,一般来说也是不提倡如此设置,尤其是文档中包含敏感信息的。
madneal
2023/09/21
2530
Google Drive 的信息检索
Google Drive网盘挂载
​ GoIndex是一款部署在Cloudflare Workers的Google Drive目录索引程序,本篇介绍如何借助GoIndex+Cloudflare挂载Google Driver
hahah
2022/08/30
4.2K0
一个方便转存 Google Drive 分享文件的方法
用过 Google Drive (以下简称GD) 的朋友们应该都清楚,GD 分享的文件可以一键添加到自己的云盘中,速度很快,一度让我感觉 Google 好牛,但仔细一看会发现这并不是将文件转存到自己的 GD 中,以大神分享的爱情公寓5资源为例:
宋天伦
2020/07/16
11.1K0
一个方便转存 Google Drive 分享文件的方法
使用Google翻译Api
将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为包含服务帐户密钥的JSON文件的文件路径。在Linux或macOS系统中设置方法如下:
职场亮哥
2020/10/10
4.6K0
Colaboratory配合Google Drive使用GPU运行机器学习代码
由于每次打开文件后台资源都是随机分配的,在运行代码之后一定要记得将结果保存。当然有的时候我们可以直接将所需文件上传到google drive上,由于资源随机分配,因此需要建立他们之间的关系。以下操作每次打开的时候,也需要重新执行。
听城
2018/10/09
1.7K0
Colaboratory配合Google Drive使用GPU运行机器学习代码
JupyterLab 与 Google Drive的完美融合!
说到云存储选项,有太多的选项可供选择:Google Drive、OneDrive和Dropbox适用于普通用户,如果你想要更安全的服务,Tresorit 或 PCloud 也可以选择。但你可能对这些都不太感兴趣,主要原因是我们想知道我的数据在哪里,而“在某个遥远的地方”并不能让我们很满意。这就是我们更热衷于家庭云解决方案的原因。
量化投资与机器学习微信公众号
2020/02/14
3.4K0
将 WebUI Colab 安装到 Google Drive
将 WebUI Colab 安装到 Google Drive Colab 页面功能 一次性安装和更新 跑步 添加模型 教程 稳定的扩散 WebUI Colab 与 Google Drive:ht
西里国际站
2023/08/23
4251
将 WebUI Colab 安装到 Google Drive
Google JavaScript API 的使用
您可以使用JavaScript客户端库与Web应用程序中的Google API(例如,人物,日历和云端硬盘)进行交互。请按照此页面上的说明进行操作。
拿我格子衫来
2022/01/24
3.1K0
如何用命令行下载Google Drive上的共享文件?
如果共享文件非常大,比如10多个G,这个时候简单的用网上推荐的 wget方法是没办法下载的,最终只是下载了一个html内容。因为当文件太大的时候谷歌会需要先跳转页面审查一下有没有病毒,然后再开始下载。
marsggbo
2022/05/11
4.4K0
如何用命令行下载Google Drive上的共享文件?
GDocs for Google Drive Mac(谷歌云端辅助软件)
需要一款谷歌云端辅助软件?GDocs for Google Docs是完美的 Google Drive 伴侣,它功能非常齐全,可让您轻松创建、查看、共享、下载、导出您的文档,从任何地方使用共享菜单在 GDocs for Google Docs 中打开 Google Docs 链接,不会再迷失在您的网络浏览器选项卡中,需要的朋友快来下载体验吧!
快乐的小丸子
2022/09/18
1.3K0
GDocs for Google Drive Mac(谷歌云端辅助软件)
GDocs for Google Drive Mac(谷歌云端辅助软件)
GDocs for Google Docs是完美的 Google Drive 伴侣,它功能非常齐全,可让您轻松创建、查看、共享、下载、导出您的文档,从任何地方使用共享菜单在 GDocs for Google Docs 中打开 Google Docs 链接,不会再迷失在您的网络浏览器选项卡中。
Mac知识分享
2022/09/16
1K0
使用Google JS api 创建 文档
https://developers.google.com/docs/api/reference/rest/v1/documents/request#Request
拿我格子衫来
2022/01/24
3.3K0
使用Google JS api 创建 文档
实战 HomeAssistant 基于 Google Drive Backup 进行备份
今天给大家介绍的是 HA 的备份,虽然官方自带有备份功能,但仅能备份至本地存储,要想备份至云端就需要使用第三方加载项了
远哥制造
2023/09/25
1.5K0
WordPress 使用插件定时备份到Google Drive等远程服务器
首先我们需要安装一个Updraft Plus插件, 然后在设置中选择将备份发送到远程,选择GoogleDrive的方法,然后需要去 http://console.developers.google.c
前Thoughtworks-杨焱
2021/12/08
8010
WordPress 使用插件定时备份到Google Drive等远程服务器
Google Drive, Onedrive, Dropbox green check marks missing; 修复 google 硬盘,同步符号错误
最近使用google 硬盘的时候,Windows平台总是出现安装后文件夹不能显示同步符号,而mac平台就无上述错误;
西湖醋鱼
2021/12/28
7010
Google Drive, Onedrive, Dropbox green check marks missing; 修复 google 硬盘,同步符号错误

相似问题

从google drive api检索文件元数据

10

迭代Team Drive文件Google Drive API JAVA

13

使用.net Google Drive api迭代google drive上的文件夹

13

Google Drive文件元数据读取

10

使用Google Drive API关联和搜索文档元数据

11
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档