const initClient = async () => {
try {
const res = await fetch('/api/get-credentials', {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
if (!res.ok) throw new Error(`Failed to fetch credentials: ${res.status}`);
const { clientId } = await res.json();
if (!clientId) {
addLog('Client ID not configured on the server');
return null;
}
const client = window.google.accounts.oauth2.initTokenClient({
client_id: clientId,
scope: 'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email',
callback: async (tokenResponse) => {
if (tokenResponse.access_token) {
setAccessToken(tokenResponse.access_token);
localStorage.setItem('access_token', tokenResponse.access_token);
const userInfo = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', {
headers: { 'Authorization': `Bearer ${tokenResponse.access_token}` },
});
const userData = await userInfo.json();
setUserEmail(userData.email);
const userRes = await fetch('/api/user', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: userData.email }),
});
const userDataResponse = await userRes.json();
addLog(userDataResponse.message);
try {
const countRes = await fetch('/api/get-pdf-count', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: userData.email }),
});
const countData = await countRes.json();
setPdfCount(countData.count || 0);
addLog(`Initial PDF count loaded: ${countData.count || 0}`);
} catch (error) {
addLog(`Failed to fetch initial PDF count: ${error.message}`);
}
markAuthenticated();
} else {
addLog('Authentication failed');
}
},
});
return client;
} catch (error) {
addLog(`Error initializing client: ${error.message}`);
return null;
}
};
This is a snippet of the code I am trying to use drive.file scope but its not working as I want. How to fix this?
Thanks!