Javascript 如何在Google Drive Api v3中获取webViewLink?
我不知道在哪里可以找到webViewLink。根据文档(),我应该在请求gapi.client.drive.files.list()时获取此参数。但我甚至没有客户端对象!也许我的方法一开始是不正确的。我在一篇文章中发现了这个解决方案。从文档来看,我不清楚如何做到这一点Javascript 如何在Google Drive Api v3中获取webViewLink?,javascript,vue.js,google-drive-api,Javascript,Vue.js,Google Drive Api,我不知道在哪里可以找到webViewLink。根据文档(),我应该在请求gapi.client.drive.files.list()时获取此参数。但我甚至没有客户端对象!也许我的方法一开始是不正确的。我在一篇文章中发现了这个解决方案。从文档来看,我不清楚如何做到这一点 mounted () { const gDrive = document.createElement('script') gDrive.setAttribute('type', 'text/javascript'
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
}
}
}
mounted(){
const gDrive=document.createElement('脚本')
setAttribute('type','text/javascript')
gDrive.setAttribute('src','https://apis.google.com/js/api.js')
document.head.appendChild(gDrive)
},
方法:{
异步pickerDialog(){
wait window.gapi.load('auth2',()=>{
window.gapi.auth2.authorize(
{
客户id:this.clientId,
范围:这个范围,
即时:假
},
这是一个很好的结果
)
})
window.gapi.load('picker',()=>{
this.pickeraproaded=true
this.createPicker()
})
},
handleAuthResult(authResult){
if(authResult&!authResult.error){
this.oauthttoken=authResult.access\u令牌
this.createPicker()
}
},
createPicker(){
if(this.pickeraproaded&&this.oauthToken){
var picker=new window.google.picker.PickerBuilder()
.enableffeature(window.google.picker.Feature.MULTISELECT\u启用)
.addView(window.google.picker.ViewId.DOCS)
.setOAuthToken(此.oauthToken)
.setDeveloperKey(this.apiKey)
.setCallback(此.pickerCallback)
.build()
选择器.setVisible(真)
}
},
异步pickerCallback(数据){
if(数据[window.google.picker.Response.ACTION]==window.google.picker.ACTION.PICKED){
const docs=data.docs
常量附件=[]
for(设i=0;iconsole.log(res))
附件.title=docs[i].名称
attachment.name=docs[i].name+'.+docs[i].mimeType.split('/')[1]
attachment.type='gDrive'
附件.description='与GDrive共享'
附件.延期=
'.' +
docs[i].mimeType.substring(docs[i].mimeType.lastIndexOf('.')+1)
附件.iconURL=docs[i].iconURL
attachment.size=docs[i].sizeBytes
attachment.user=JSON.parse(localStorage.getItem('user'))
attachment.thumb=null
attachment.thumb_list=null
附件。推送(附件)
}
this.tempAttachments=[…附件]
}
this.oauthToken=null
this.pickeraproaded=false
}
}
}
执行列表请求时,请指定属性
就你而言:
返回gapi.client.drive.files.list({
“字段”:“文件/webViewLink”
})
要执行gapi.client.drive.files.list()
请求,请遵循for-drive API,该API可用于客户端Javascript和node.js
另一种检索webViewLink
——如果您知道fileId
,并且对vue.js更可行,那么将其硬编码为
var-webcontentlink='1〕https://docs.google.com/uc?id=“+fileId+”&export=download'
但是我在gapi APIs中没有客户机对象。那么到目前为止如何使用gapi.client.drive.files.list()
呢?我不使用此方法,因为出于某种原因,我在客户机对象中没有驱动器对象(客户端对象出现在我在授权过程中将gapi.auth2.authorize更改为gapi.auth.authorize之后。