简体   繁体   English

使用API​​对元数据进行迭代的Google Drive文件

[英]Iterating Google Drive files using API for metadata

I want to display drive file metadata 我想显示驱动器文件元数据

"iconLink", "thumbnailLink" “ iconLink”,“ thumbnailLink”

of each of the files in drive, but getting output for fields like kind, id, name, mimeType only. 驱动器中每个文件的名称,但仅获取种类,id,名称,mimeType等字段的输出。 While other fields are not displayed. 而其他字段未显示。

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.');
        }
    });
}

at least you will need the scope: https://www.googleapis.com/auth/drive.metadata.readonly + OAuth (API-Key & Client-ID) 至少您将需要以下范围: https : //www.googleapis.com/auth/drive.metadata.readonly + OAuth(API密钥和客户端ID)

you can test it at: https://developers.google.com/drive/v3/reference/files/list 您可以在以下位置对其进行测试: https : //developers.google.com/drive/v3/reference/files/list

in the "fields" input add: files(iconLink,thumbnailLink) 在“字段”输入中添加:files(iconLink,thumbnailLink)

if you use https://apis.google.com/js/api.js , be sure to add your domain to API-Key -> HTTP-Referrer & Client-ID -> JavaScript-Source & Forwarding-URI (@ https://console.developers.google.com/apis/credentials ) 如果您使用https://apis.google.com/js/api.js ,请确保将您的域添加到API-Key-> HTTP-Referrer&Client-ID-> JavaScript-Source&Forwarding-URI(@ https ://console.developers.google.com/apis/credentials

you can find a basic gapi usage sample here: https://github.com/google/google-api-javascript-client/blob/51aa25bed4f6c36d8e76fd3b9f7e280ded945c98/samples/loadedDiscovery.html 您可以在此处找到基本的gapi使用示例: https//github.com/google/google-api-javascript-client/blob/51aa25bed4f6c36d8e76fd3b9f7e280ded945c98/samples/loadedDiscovery.html

I promisified the gapi client a bit some time ago, because I disliked the mix of callbacks and thenables in the methods.. this worked for me (assuming api.js was already loaded), but only hold 100 file entries in the response. 不久前,我承诺了gapi客户端,因为我不喜欢方法中回调和thenables的混合..这对我有用(假设api.js已经加载),但响应中仅包含100个文件条目。

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)
  })
})

In the v3 you need to specify which fields you want included in the metadata response. 在v3中,您需要指定要包含在元数据响应中的字段。 See the fields= parameter 请参阅fields=参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM