1

I don't receive an error, but the Javascript code just stops executing when I run the following function:

function initGoogleURL() {
    gapi.client.setApiKey(myAPIKey);
    gapi.client.load('urlshortener', 'v1').then(function(value) {
        console.log("success!");
        googleReady = true; // Success!
    }, function(reason) {
        console.log(reason); // Error!
    });
}

I don't see a message in the console. And the rest of the code stops executing, even though it's not dependent on this path of execution.

Any ideas why this is happening?

Matt Brand
  • 346
  • 1
  • 4
  • 17

1 Answers1

0

First of all - the fact that you are getting no message in the console is concerning. Are you sure you're executing the code? Do you have the line

initGoogleURL();

somewhere in your code? Assuming you do, I think the issue is similar to the one described here. My rational for this is if I set a timeout on the function, for example:

setTimeout(function(){
  initGoogleURL();
}, 5000) // Second argument is time to wait before executing the function

then, I get a success message printed out. If you see similar behavior, your issue is that you are trying to use the google api before it has been fully loaded. The resolution to this is to define a callback handler for when the API library is successfully loaded, as prescribed in your import statement in HTML, e.g:

<script src="https://apis.google.com/js/client.js?onload=OnLoadCallback"></script>
Community
  • 1
  • 1
  • Yes, very concerning. I set this up on a site where it worked fine, migrated it to another site, and it's not working as described. Both sites are in the section "Accept requests from these HTTP referrers (web sites)" in Google Developer Console for this API. I tried calling the function as you described, using onload=, and it fails right there too, once it hits line "gapi.client.load..." – Matt Brand Nov 24 '15 at 15:32
  • And I'm sure I'm executing the code, because if I put a console.log as the first line of the function, I get that log. – Matt Brand Nov 24 '15 at 17:52