使用gapi.client javascript执行我的自定义Google API [英] Use gapi.client javascript to execute my custom Google API

查看:148
本文介绍了使用gapi.client javascript执行我的自定义Google API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务已成功部署到Google端点,并且可以通过浏览器访问.

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

现在,我正在尝试加载Google API javascript客户端库以使用javascript调用我的服务.

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) {...});
   );

但是我总是在运行时遇到异常,抱怨 gapi.client.[MY_API_NAME] 未定义.我对任何Google API(例如Plus)都做同样的事情,并且工作正常.例如,如果我加载"plus" API,则可以访问gapi.client.plus ...,并且可以调用方法.

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.

我错过了什么吗?所有示例和文档都是关于Google Service API的,我找不到用于自定义API(开发人员编写的API)的示例.

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).

我什至尝试使用不同的路径(绝对路径和相对路径)来执行gapi.client.request,但出现404-状态"中未找到错误.

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) {...});

推荐答案

问题是调用gapi.client.load()时缺少参数.

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

我通过此链接 https:/查看了gapi.client.load的定义./developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientload

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

后来我发现它并不完全正确,并且缺少可选参数( app_api_root_url ).

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)

如果缺少app_api_root_url,则仅为Google服务API加载客户端(app_api_root_url,例如 https://myapp.appspot. com/_ah/api )

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)

您可以在此链接 https中找到有关如何正确使用gapi.client.load()的更多详细信息. ://developers.google.com/appengine/docs/java/endpoints/consume_js

如下面的代码所示,当我调用 gapi.client.load 时,我没有 ROOT 参数,这就是Google通过默认情况下,它正在查看自己的服务API,但显然找不到我的API.

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);

注意: your_app_id 仅在ROOT参数中用于加载客户端脚本.加载完成后,您将拥有一个以您的API命名的对象,而不是您的应用程序.该对象就像您的Java(服务)类,您可以用来直接调用方法.

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.

这篇关于使用gapi.client javascript执行我的自定义Google API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆