Post's cover


I wanted to give up countless times. You never know how hopeless I was every time I found a Google Drive API tutorial video on YouTube not very helpful. Also, there're fewer related posts about how to implement Google Drive API with React. It did take me a few days to figure them out and record some important details as much as I can remember in this blog.

H1Google OAuth2

Using react-oauth can make your life easier.


jsx<GoogleOAuthProvider clientId="..."> <App /> </GoogleOAuthProvider>


jsximport { useGoogleLogin } from "@react-oauth/google"; const login = useGoogleLogin({ onSuccess: async (res) => { // localStorage.setItem("access_token", res.access_token); }, onError: () => { console.log("Login Failed"); }, scope: "", });

Note: Don't forget to set scope (A space-delimited list of scopes. The type is String). Otherwise, you have no permission to use some Google APIs.


jsximport { googleLogout } from "@react-oauth/google"; const logout = () => { googleLogout(); // localStorage.removeItem("access_token"); };

H2Get UserInfo

jsxuseEffect(() => { if (token) { axios .get("", { headers: { Authorization: `Bearer ${token}` }, }) .then((userInfo) => { // setUser(; }) .catch((err) => { // localStorage.removeItem("access_token"); console.error(err); }); } }, [token]);

H1Google Drive & Picker

H2Set up

Follow Drive API - JavaScript quickstart doc to set up and run an app that calls a Google Workspace API.

H2Get file from Google Drive



jsxconst handleOpenPicker = () => { openPicker({ clientId: "...", developerKey: "...", viewMimeTypes: "application/json", token: token, supportDrives: true, callbackFunction: (data) => { switch (data.action) { case "cancel": console.log("User clicked cancel/close button"); break; case "picked": onFileUpload( `${[0].id}?alt=media&key=...`,[0].name ); break; default: } }, }); };

Note: You can get token after you login succesfully.

Get file from Google Drive (In this scenario, the file I want to get is a JSON file.):

jsxconst onFileUploadDrive = (url, name) => { localStorage.setItem("cv_data_file_name", name); axios .get(url, { headers: { Authorization: `Bearer ${localStorage.getItem("access_token")}`, Accept: "application/json", }, }) .then((res) => { // console.log(; }) .catch((error) => { console.error("Fetch error:", error); }); };

H2Upload file to Google Drive


jsxconst onSaveDrive = () => { const token = localStorage.getItem("access_token"); if (token) { const jsonData = JSON.stringify(cvData); const file = new File([jsonData], fileName); axios .post( ``, file, { headers: { Authorization: `Bearer ${token}`, Accept: "application/json", "Content-Type": "application/json", }, } ) .then((res) => { axios .patch( `${}?key=...`, { name: fileName, description: "This CV metadata is downloaded from", }, { headers: { Authorization: `Bearer ${token}`, Accept: "application/json", "Content-Type": "application/json", }, } ) .then((res1) => { setIsShowSave(false); setOpenUpload(true); }); }) .catch((error) => { console.error("Fetch error:", error); }); } else { setOpenSignIn(true); } };

The reason why there are two requests to be send is because I can't find a way to upload a JSON file to Goole Drive with a file name. So my strategy is upload a Untitled file to Drive and then update its file name.


H1Make it public

If you want some Google API services on your deployed website to work properly, you need to make it public in OAuth consent screen.

The process of publishing your app is not very hard but troublesome. They need you to make a YouTube video to explain how to secure user data. Yeh I kinda quit. I know nobody's gonna use my web app hahah unless I advertise it.



Related Posts