I'm attempting to retrieve data (folder Name, ID, type, etc) from my Google Drive using the googleapis library
Here's the code I am using:
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 successfully retrieves data, it seems to only return data that I have previously uploaded using an upload function. What I actually want is to retrieve all existing data in my drive such as folder name, id, mimetype, etc.
I am utilizing an API route for authentication as follows:
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 assist me with understanding why I am not getting the desired results? Thanks!