Search code examples
javascripttypescriptnext.jsgoogle-api

Getting the raw data from google drive using google api


I'm trying to make a "GET" data(folder Name, ID , type etc) from my google drive using googleapis

I'm using this code

const fetchGoogleDriveFiles = async () => {
    try {
      const response = await fetch(
        `https://www.googleapis.com/drive/v3/files?q=mimeType='application/vnd.google-apps.folder'&fields=files(id,name,mimeType,parents)`,
        {
          method: "GET", 
          headers: {
            Authorization: `Bearer ${session.accessToken}`,
          },
        }
      ); 
      if (response.ok) {
        const data = await response.json();
        console.log("Google Drive Files:", data.files);
      } else {
        console.error("Error fetching Google Drive files:", response.status);
      }
    } catch (error) {
      console.error("Error fetching Google Drive files:", error);
    }
  };

while this code, do get data, however the data it is "GETTING" is data the i have previously uploaded using a upload function. What i want and plan to do is, get all the data that already exists inside my drive such as folder name, id, memtype etc etc

im using a API route for my authentication and such import NextAuth from "next-auth/next" import GoogleProvider from "next-auth/providers/google"

export const authOptions = ({
    providers: [
        GoogleProvider({
            clientId : process.env.GOOGLE_CLIENT_ID ?? "" ,
            clientSecret : process.env.GOOGLE_CLIENT_SECRET ?? "",
            authorization: { 
              params: 
              { 
                scope: "openid email profile https://www.googleapis.com/auth/drive.file" 
                //
              } 
            },
        })
    ],
    callbacks: {
      async jwt({token, user , account} : any){  
        if (account) {
          token.accessToken = account.access_token;
        }
        return token
      },
      async session({ session, token } : any) {
        const newSession = {
          ...session,
          accessToken: token.accessToken,
        }; 
        return newSession;
      },
    }
})
 
const handler = NextAuth(authOptions)
export {handler as GET, handler as POST}

can anyone help me out here? i dont seem to get it properly


Solution

  • Your scope of https://www.googleapis.com/auth/drive.file retrieves only the files and folders that were created by the same client.

    When you want to achieve your goal of get all the data that already exists inside my drive such as folder name, id, memtype etc etc, it is required to change the scope as follows.

    From:

    https://www.googleapis.com/auth/drive.file
    

    To:

    https://www.googleapis.com/auth/drive.metadata.readonly
    

    In this scope, only the file metadata of all files and folders is returned. If you want to retrieve both the file metadata and the file content, please use https://www.googleapis.com/auth/drive.readonly. If you want to upload and download the file content and the file metadata including all files and folders, please use https://www.googleapis.com/auth/drive.

    Note:

    • As additional information, in your query parameters, the metadata of only 100 folders is returned. Ref If you want to retrieve all files and folders, please use pageSize and pageToken. Please be careful about this.

    Reference: