I'm trying to create an album on my react website for a friend of mine, and I want the images inside the album to be connected to a google drive so that he can upload new images on his own.
I managed to add the script inside app scripts:
var filelinks = [];
var mimetypes = ["image/png","image/jpeg","image/gif","image/bmp"];
function doGet(e) {
var fid;
if(e.parameters.fid==undefined){
fid='ID_OF_THE_GOOGLE_DRIVE_ALBUM';
} else {
fid=e.parameters.fid[0];
}
var data = travseItems(fid);
return buildSuccessResponse(data, 1);
}
function travseItems(folderid){
var folder = DriveApp.getFolderById(folderid);
var files=folder.getFiles();
var x=0;
while(files.hasNext()){
var file = files.next();
if( mimetypes.indexOf(file.getMimeType()) != -1){
var filelink ={};
filelink['img_id']=file.getId();
filelink['name']='';
filelink['folder_id']='';
console.log(filelink['img_id']+"---folder:"+filelink['folder_id']+"\n\n");
filelinks.push(filelink);
}
} //is file
return filelinks;
}
function buildSuccessResponse(posts, pages) {
var output = JSON.stringify({
status: 'success',
data: posts,
pages: pages
});
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}
When I click the link provided after creating the script I can see the array with all the images.
but when I try make the fetch call on my application I get an error saying:
... Access to fetch at 'THE_LINK_PROVIDED_BY_GOOGLE_API' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
this is the code inside my react app:
const WEB_APP_URL = 'THE_LINK_PROVIDED_BY_GOOGLE_API'
useEffect(() => {
fetch(WEB_APP_URL)
.then(response => response.json())
.then(obj => {
const list = obj.data;
for (let i = 0; i < list.length; i++) {
const url_split = list[i].book_url.split('/d/');
const image_id = url_split[1].split('/view');
console.log(image_id[0]);
}
});
}, [])
Any ideas on how to solve this?