I'm having trouble locating the webViewLink. The documentation (https://developers.google.com/drive/api/v3/reference/files) states that I should receive this parameter when requesting gapi.client.drive.files.list(). However, I don't even have a client object! It seems like my approach might have been wrong from the start. I found a potential solution in an article, but the documentation doesn't provide clear instructions on how to proceed.
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
}
}
}