function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Learn Salesforce 36Learn Salesforce 36 

Can't load google api client in lwc

Getting error on below line 
gapi.load('client', () => {});
Error is below
gapi load error TypeError: Cannot read properties of undefined (reading 'error')

Below is the js code for reference:
import { LightningElement, track } from 'lwc';
import pdflib from '@salesforce/resourceUrl/pdflib';
import driveapi from '@salesforce/resourceUrl/driveapi';
import driveclient from '@salesforce/resourceUrl/driveclient';
import driveClientId from '@salesforce/label/c.drive_client_id';
import driveApiKey from '@salesforce/label/c.drive_api_key';
import { loadScript } from 'lightning/platformResourceLoader';
export default class Resumebuilder extends LightningElement {
    @track name;
    @track contact;
    @track email;
    @track downloadUrl;    
    renderedCallback() {
      loadScript(this, pdflib).then(() => {});  
      loadScript(this, driveclient).then(() => {
        console.log('drive client loaded');
        google.accounts.oauth2.initTokenClient({
         client_id: driveClientId,
         scope: 'https://www.googleapis.com/auth/drive.file'         
       });

       loadScript(this, driveapi).then(() => {
        console.log('load drive api'+gapi);
        try{
          gapi.load('client', () => {
            console.log('in load client');            
          });
        } catch (e){
          console.log('gapi load error '+e);  //getting the error here
        }       
    });
      }).catch(e => {
        console.log('client load error '+e);
      });      
    }

Note: This lwc is added in experience site page.
Arun Kumar 1141Arun Kumar 1141
Hi,

The gapi object is undefined and lacks an error property, as indicated by the error message "TypeError: Cannot read properties of undefined (reading 'error')". This may indicate that the Google API client script was not properly loaded.

According to your code, it appears that you are loading the Google API client script using loadScript from the lightning/platformResourceLoader module. Check the following items:


Make that the Google API client script has been added to your Salesforce org as a static resource and that loadScript is utilising the right URL. This can be confirmed by looking at the script's URL in the definition of the static resource.

Check the network tab in your browser's developer tools to make sure the script is loading properly. A network request to the script's URL should be watched for, and the response status should be 200 (OK).

As soon as the script has loaded, make sure the gapi object is accessible. You can accomplish this by following the loadScript call with a console.log(gapi) command.

If the abovementioned measures fail to cure the problem, there can be a problem with the Google API client script itself. Alternate script versions can be used, or you can ask the Google API support staff for help.

Thanks
Learn Salesforce 36Learn Salesforce 36
Hi Arun,

I checked, script is loaded in the network and gapi object is accessible but issue is still there.

I observed one more thing, when lightning web security is enabled then I am getting above issue. but if I disable it then above issue gets resolved but then I face a different issue and 'driveclient script doesn't get loaded.