fix: Resolve Google Drive picker API loading and token handling

This commit is contained in:
Taylor Wilsdon (aider) 2024-12-15 18:45:40 -05:00
parent 3378f35296
commit aaacd28131
2 changed files with 15 additions and 15 deletions

View File

@ -501,15 +501,9 @@
try { try {
const fileData = await createPicker(); const fileData = await createPicker();
if (fileData) { if (fileData) {
// Pass the OAuth token along with the file data
dispatch('upload', { dispatch('upload', {
type: 'google-drive', type: 'google-drive',
data: { data: fileData // fileData now includes token
name: fileData.name,
url: fileData.url,
id: fileData.id,
token: await getAuthToken() // Include OAuth token for download
}
}); });
} }
} catch (error) { } catch (error) {

View File

@ -23,20 +23,24 @@ export const loadGoogleDriveApi = () => {
const script = document.createElement('script'); const script = document.createElement('script');
script.src = 'https://apis.google.com/js/api.js'; script.src = 'https://apis.google.com/js/api.js';
script.onload = () => { script.onload = () => {
gapi.load('picker', { gapi.load('client:picker', {
callback: () => { callback: () => {
pickerApiLoaded = true; gapi.client.load('picker', 'v1').then(() => {
resolve(true); pickerApiLoaded = true;
resolve(true);
});
} }
}); });
}; };
script.onerror = reject; script.onerror = reject;
document.body.appendChild(script); document.body.appendChild(script);
} else { } else {
gapi.load('picker', { gapi.load('client:picker', {
callback: () => { callback: () => {
pickerApiLoaded = true; gapi.client.load('picker', 'v1').then(() => {
resolve(true); pickerApiLoaded = true;
resolve(true);
});
} }
}); });
} }
@ -112,11 +116,13 @@ export const createPicker = () => {
const fileUrl = doc[google.picker.Document.URL]; const fileUrl = doc[google.picker.Document.URL];
// Get the downloadUrl using the alt=media parameter // Get the downloadUrl using the alt=media parameter
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`; // Construct download URL with access token
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media&access_token=${oauthToken}`;
resolve({ resolve({
id: fileId, id: fileId,
name: fileName, name: fileName,
url: downloadUrl url: downloadUrl,
token: oauthToken // Include token for future use
}); });
} else if (data[google.picker.Response.ACTION] === google.picker.Action.CANCEL) { } else if (data[google.picker.Response.ACTION] === google.picker.Action.CANCEL) {
resolve(null); resolve(null);