Omar Obeid
Omar Obeid

Reputation: 11

Uncaught TypeError: Cannot read property 'signIn' of null (When running the code on localhost!)

I ran this code on "js.do" and it worked without any issues. When I ran it on a localhost, however, I got the following error: "Uncaught TypeError: Cannot read property 'signIn' of null".

<script src="https://apis.google.com/js/api.js"></script>
<script>

      var CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
      var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';

  /**
   * Sample JavaScript code for reseller.subscriptions.list
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/guides/code_samples#javascript
   */

  function authenticate() {
    return gapi.auth2.getAuthInstance()
    .signIn({scope: "https://www.googleapis.com/auth/apps.order https://www.googleapis.com/auth/apps.order.readonly"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }

  function loadClient() {code to load client  }
  // Make sure the client is loaded and sign-in is complete before calling this method.

  function execute() {// code to execute API read commands  }

  function checkStatus (response,counter)
  {
  console.log("inside checkStatus",response.seats);
  return response.seats.numberOfSeats > 0;
  }
  
  gapi.load("client:auth2", function() {
    gapi.auth2.init({
        client_id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    });
    
  });
  
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

What could be the reason for this?

Upvotes: 0

Views: 2924

Answers (2)

Omar Obeid
Omar Obeid

Reputation: 11

The gapi.load("client:auth2", function() {...}) is what's causing issue. I'm getting this error "gapi.auth2.ExternallyVisibleError: Invalid cookiePolicy" because I was trying to test the API by directly acessing my files locally (index.html). The Google Sign In API only works in a running web server, as answered in the following post (click here)

Upvotes: 1

Marius Franz&#233;n
Marius Franz&#233;n

Reputation: 25

I'm assuming it's the signIn in the authenticate function. You could try and do console.log(gapi.auth2.getAuthInstance()) before the return and see what that returns. Might give you a clue.

Upvotes: 0

Related Questions