簡體   English   中英

如何在 Google Drive Api v3 中獲取 webViewLink?

[英]How to get the webViewLink in Google Drive Api v3?

我不知道在哪里可以找到 webViewLink。 根據文檔( https://developers.google.com/drive/api/v3/reference/files ),我應該在請求 gapi.client.drive.files.list() 時獲取此參數。 但我什至沒有客戶 object。 也許我的方法最初是不正確的。 我在一篇文章中發現了這個解決方案,來自文檔。 我不清楚如何做到這一點。

  mounted () {
   const gDrive = document.createElement('script')
    gDrive.setAttribute('type', 'text/javascript')
    gDrive.setAttribute('src', 'https://apis.google.com/js/api.js')
    document.head.appendChild(gDrive)
  },

  methods: {
   async pickerDialog () {
      await window.gapi.load('auth2', () => {
        window.gapi.auth2.authorize(
          {
            client_id: this.clientId,
            scope: this.scope,
            immediate: false
          },
          this.handleAuthResult
        )
      })

      window.gapi.load('picker', () => {
        this.pickerApiLoaded = true
        this.createPicker()
      })
    },

    handleAuthResult (authResult) {
      if (authResult && !authResult.error) {
        this.oauthToken = authResult.access_token
        this.createPicker()
      }
    },

    createPicker () {
      if (this.pickerApiLoaded && this.oauthToken) {
        var picker = new window.google.picker.PickerBuilder()
          .enableFeature(window.google.picker.Feature.MULTISELECT_ENABLED)
          .addView(window.google.picker.ViewId.DOCS)
          .setOAuthToken(this.oauthToken)
          .setDeveloperKey(this.apiKey)
          .setCallback(this.pickerCallback)
          .build()

        picker.setVisible(true)
      }
    },

    async pickerCallback (data) {
      if (data[window.google.picker.Response.ACTION] === window.google.picker.Action.PICKED) {
        const docs = data.docs
        const attachments = []
        for (let i = 0; i < docs.length; i++) {
          const attachment = {}
          attachment._id = docs[i].id
          this.$axios.get(`https://www.googleapis.com/drive/v3/files/${attachment._id}`, {
            headers: {
              Authorization: `Bearer ${this.oauthToken}`
            }
          }).then(res => console.log(res))

          attachment.title = docs[i].name
          attachment.name = docs[i].name + '.' + docs[i].mimeType.split('/')[1]
          attachment.type = 'gDrive'
          attachment.description = 'Shared with GDrive'
          attachment.extension =
            '.' +
            docs[i].mimeType.substring(docs[i].mimeType.lastIndexOf('.') + 1)
          attachment.iconURL = docs[i].iconUrl
          attachment.size = docs[i].sizeBytes
          attachment.user = JSON.parse(localStorage.getItem('user'))
          attachment.thumb = null
          attachment.thumb_list = null
          attachments.push(attachment)
        }
        this.tempAttachments = [...attachments]
      }
      this.oauthToken = null
      this.pickerApiLoaded = false
    }
  }
}

當您執行列表請求時,請指定屬性字段

在你的情況下:

 return gapi.client.drive.files.list({
      "fields": "files/webViewLink"
    })

要執行gapi.client.drive.files.list()請求 - 請遵循 Drive API 的快速入門,該快速入門可用於客戶端 Javascript 和 node.js。

檢索webViewLink的另一種方法 - 如果您知道 fileId 並且對於fileId更可行,則將其硬編碼為

var webcontentlink = ' https://docs.google.com/uc?id='+fileId+'&export=download'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM