I would like to host around 300 photo on Google drive and be able to fetch every photo on my react project, but I don't know how to start like should I need a back-end like node.js? I've seen this video (https://www.youtube.com/watch?v=iajv8y-6n3Q), but he displays only 1 photo.
I wanna display all my images from Google drive on the React and put it on a array like this following JSON
const photos = [
{
src: 'https://source.unsplash.com/2ShvY8Lf6l0/800x599',
width: 4,
height: 3,
year: '2021',
event: 'Hiking',
},
{
src: 'https://source.unsplash.com/Dm-qxdynoEc/800x799',
width: 1,
height: 1,
year: '2021',
event: 'J-On',
},
{
src: 'https://source.unsplash.com/qDkso9nvCg0/600x799',
width: 3,
height: 4,
year: '2021',
event: 'Language Exchange',
},
and so on...
You can achieve your goal in two ways:
Next.jsapp andgoogleapis.1. Get the files link by manually copying the link from google drive:
Get Linkand a pop up will openGeneral AccessselectAnyone with the linkand make sure the role isViewerCopy Linkand done.Then paste it at any text editor, and you will get the link like this:
Remove the
file/dand replace it withuc?export=view&id=and remove the/view?usp=sharingcompletely and you will get something like:And put the image information in an array:
Please do the same with the other files, and your images constant finally looks like this:
Now, it is time to render the images in our React Application by using javascript's map method:
2. Fetch the data by using
Next.jsapp andgoogleapis.In case we have many photos, it is better to use data fetching. Here we use Next.js app and googleapis library.
There are 3 pieces of information that we need to provide in order to be able to fetch data from
https://www.googleapis.com/drive/v2server which are:For
CLIENT_ID&CLIENT_SECRETare OAuth 2.0 Client information that can be obtained from Google Developers Console. You can read the details here. And for theREFRESH_TOKENcan be obtained from OAuth 2.0 Playground and the steps are as follows:OAuth 2.0 ConfigurationbuttonUse your own OAuth credentialscheckboxhttps://www.googleapis.com/auth/driveto enable the Authorize APIs button.Authorize APIsbutton.Sign in with googlepage. Select the account that we used to generate the CLIENT_ID & CLIENT_SECRET.Google hasn’t verified this apppage appears, just click Advance and click Go to ...(unsafe) and select the access types that will be granted to our Next.js app then clickContinueand you will be redirected to OAuth 2.0 Playground again.Exchange authorization code for tokensbutton atStep 2 Exchange authorization code for tokens, and click it to get theRefresh tokenandAccess tokenhttps://www.googleapis.com/drive/v2/files?access_token={ACCESS_TOKEN}in your browser. Our configuration is successful if there is a raw JSON page that shows our drive items.Now, we are ready for coding. We will use our NextJs API to communicate with
googleapisserver and serve the data to be consumed by our app.Open your Nextjs app, create a
/pages/api/drive-files.tsfile, and importgoogleapislibrary (here we use Typescript).Create types:
Add the Data type at the NextApiResponse and the handler will look like this:
Next, create
oauth2Clientthat use CLIENT_ID and CLIENT_SECRET credentials and we usehttps://developers.google.com/oauthplaygroundas the redirect uri,setCredentialsusing REFRESH_TOKEN, and return thegoogle.driveresponse as JSON;And when you access
http://localhost:3000/api/drive-filesfrom the browser, you will get:Since we will use NextJs
Imagecomponent, don't forget to add domaindrive.google.comat next.config.js:Finally, you can render the images as follows: