Question

I have a service that is successfully deployed to Google Endpoints and it is accessible through browser.

Now I am trying to load Google API javascript client library to call my services using javascript.

As far as I know, I should do this

gapi.client.load([MY_APP_NAME], 'v1', function() {
   var request = gapi.client.[API_NAME].[SERVICE_NAME].[METHOD]();
   request.execute(function(jsonResp, rawResp) {...});
   );

But I always get an exception at run time complaining about gapi.client.[MY_API_NAME] is undefined. I do the same thing with any Google API (such as Plus) and it works fine. For example, If I load 'plus' API, I will have access to gapi.client.plus... and I can call methods.

Am I missing something? All samples and documents are about Google Service APIs and I could not find a sample for custom APIs (the one that developers write).

I even tried gapi.client.request with different paths (absolute path and relative path) but I get 404 - Not Found error in "status".

var request = gapi.client.request({'path':
'https://[APP_NAME].appspot.com/_ah/api/[SERVICE_NAME]/v1/[METHOD]'
, 'method': 'GET'});
request.execute(function(jsonResp, rawResp) {...});


var request = gapi.client.request({
'path':'/[SERVICE_NAME]/v1/[METHOD]',
'method': 'GET'});
request.execute(function(jsonResp, rawResp) {...});
Was it helpful?

Solution

The problem was a missing parameter in calling gapi.client.load().

I looked at the definition of gapi.client.load at this link https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientload

gapi.client.load(name, version, callback)

which then later I found out is not totally correct and an optional parameter is missing (app_api_root_url).

gapi.client.load(name, version, callback, app_api_root_url)

If the app_api_root_url is missing, the client is loaded for Google Service APIs only (app_api_root_url such as https://myapp.appspot.com/_ah/api)

You can find more details on how to use gapi.client.load() properly at this link https://developers.google.com/appengine/docs/java/endpoints/consume_js

As you can see in the following piece of code, I didn't have ROOT parameter when I was calling gapi.client.load and that is why Google by default was looking at its own service API and obviously could not find my APIs.

   var ROOT = 'https://your_app_id.appspot.com/_ah/api';

   gapi.client.load('your_api_name', 'v1', function() {

      var request = gapi.client.your_api_name.your_method_name();
      request.execute(function(jsonResp, rawResp) {
                        //do the rest of what you need to do
                      });

   }, ROOT);

NOTE: your_app_id is used in ROOT parameter only to load the client script. After loading is done, you will have an object that is named after your API and not your app. That object is like your Java (service) class and you can use to invoke methods directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top