const developerKey = 'AIzaSyDr30lsukCywJXYYPSn5prs6FwvmgqQkxg' const clientId = '762288849553-blsuhbaecksbh4tsi5blkhvp2hq9beo5.apps.googleusercontent.com' const appId = '762288849553' const scope = [ 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive.appdata', ] let pickerApiLoaded = false let oauthToken const loadPicker = () => { gapi.load('auth', { callback: onAuthApiLoad }) gapi.load('picker', { callback: onPickerApiLoad }) } const onAuthApiLoad = () => { window.gapi.auth.authorize( { client_id: clientId, scope: scope, immediate: false, }, handleAuthResult ) } let onPickerApiLoad = () => { pickerApiLoaded = true createPicker() } const handleAuthResult = (authResult) => { if (authResult && !authResult.error) { oauthToken = authResult.access_token createPicker() } } const createPicker = () => { if (pickerApiLoaded && oauthToken) { const view = new google.picker.View(google.picker.ViewId.DOCS) view.setMimeTypes('application/pdf,image/jpeg,image/png,image/jpg') const picker = new google.picker.PickerBuilder() .enableFeature(google.picker.Feature.NAV_HIDDEN) .enableFeature(google.picker.Feature.MULTISELECT_ENABLED) .setAppId(appId) .setOAuthToken(oauthToken) .addView(view) .addView(new google.picker.DocsUploadView()) .setDeveloperKey(developerKey) .setCallback(pickerCallback) .build() picker.setVisible(true) } } const pickerCallback = async (data) => { if (data.action == google.picker.Action.PICKED) { const res = await fetch( `https://www.googleapis.com/drive/v3/files/${data.docs[0].id}?alt=media`, { headers: { Authorization: `Bearer ${oauthToken}`, }, } ).then((res) => res.blob()) let metadata = { type: data.docs[0].mimeType, } let file = new File([res], data.docs[0].name, metadata) getFile(file) } } function chooseFromDropbox() { Dropbox.choose({ success: async (files) => { if (files.length < 0) { return } var file = files[0] const url = new URL(file.link) const response = await fetch(url).then((res) => res.blob()) let metadata = { type: response.type, } let newFile = new File([response], file.name, metadata) getDropBoxFile(newFile) }, cancel: function () {}, linkType: 'direct', //Direct = Download link multiselect: false, //Allow to select one file extensions: ['.jpg'], //Allow to choose PDF file only folderselect: false, //Not allow to select folder }) }