79564314

Date: 2025-04-09 12:22:55
Score: 6.5 🚩
Natty:
Report link
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!

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (1): its not working
  • Blacklisted phrase (1): I am trying to
  • RegEx Blacklisted phrase (1.5): How to fix this?
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: Theaccountantguy