Datta Prabhu M
Datta Prabhu M

Reputation: 21

ReferenceError:gapi is not defined

I am seeing ReferenceError:gapi is not defined error when accessing the Google+ APIs from my HTML code.

My code is very simple, but not sure what is wrong with it.

Any inputs would be highly appreciated.

Please see my code below:

<html>
    <head>
        <title> Some title here </title>
        <script src="https://apis.google.com/js/platform.js"></script>
        <script>
            function handleClientLoad(){
                gapi.client.setApiKey('MY API KEY HERE')};
        </script>
        <script src="https://apis.google.com/js/client.js?  onload=handleClientLoad"></script>
    </head>
    <body>
    </body>
</html>

Error is

Execution failed: ReferenceError: "gapi" is not defined

Upvotes: 1

Views: 3559

Answers (2)

Datta Prabhu M
Datta Prabhu M

Reputation: 21

I was able to fix the issue. I should have added more details to the question but didnt thought that the issue could be somewhere else. Sorry for the same.

When we create a Web App project in Google Drive, the javascript code which runs on the server and the code which runs on the client(a browser) should be specified in different files. These files are Code.gs and JavaScript.html respectively. "gapi" is a client code (defined in client.js) and I was referencing to it in the server file. Since server code is running in the Google servers, it has no clue about our client.js and platform.js libraries. Hence the error. When I moved the error causing code to JavaScript.html, it worked fine.

Thanks.,

Upvotes: 1

David
David

Reputation: 1774

I'm not sure if this is the answer, but I notice that there is a space between "client.js?" and "onload=". This might not be in your original code, but it does cause the client.js request to return different code.

Upvotes: 3

Related Questions