【问题标题】:How to call App Engine Endpoints with the JavaScript library in promises mode如何在 Promise 模式下使用 JavaScript 库调用 App Engine Endpoints
【发布时间】:2016-05-30 14:41:34
【问题描述】:

我有一个使用 Google API JavaScript 客户端库调用多个 App Engine Endpoints 的 Web 应用程序。

按照 Google (https://developers.google.com/api-client-library/javascript/features/promises#using-promises) 的建议,我目前正在将此应用程序从回调模式更改为承诺模式,但我遇到了问题。请注意,该应用在回调模式下运行良好。

我对 promises 模式的问题是在调用请求方法时找到正确的路径参数

JavaScrit 代码:

var params = {'webSafeKeyParent’: ‘neN4fm15xW52b2ljZXMtb19saW5lmlYLEglBY1NFwpRpdHkYgICAgQj97AoM’};
gapi.client.request({
      'path': 'https://myappenginename.appspot.com/_ah/api/customerApi/v1/?????????',
      'params': params
}).then(function(response) {
        // Handle response       
}, function(reason) {
        // Handle error
});

“customerApi”中的端点定义:

@ApiMethod(
        name = "listByParent",
        path = "customerByParent/{webSafeKeyParent}",
        httpMethod = ApiMethod.HttpMethod.GET,
        scopes = {Constants.EMAIL_SCOPE},
        clientIds = {Constants.WEB_CLIENT_ID, com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
        audiences = {Constants.ANDROID_AUDIENCE})
public List<Customer> listByParent(final User user, @Named("webSafeKeyParent") final String webSafeKeyParent, @Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) throws UnauthorizedException {

对于我的少数端点,它通过在 JavaScript 请求方法的路径参数中包含 @ApiMethod 注释中声明的“路径”和“名称”的值来工作。

即对于上述端点,以下路径有效: https://myappenginename.appspot.com/_ah/api/customerApi/v1/customerByParent/listByParent

奇怪的是,这不适用于某些其他同类端点。我收到 404 HTTP 错误或 503 错误。

当您使用 APIs Explorer 查询端点但没有成功时,我也尝试使用“请求”下显示的路径......

是否有关于如何使用 Google API JavaScript 客户端库调用 App Engine Endpoints 的详细文档?我还没有找到。您有什么建议可以分享吗?

提前致谢

【问题讨论】:

    标签: google-app-engine google-cloud-endpoints


    【解决方案1】:

    实际上,请求方法始终使用由 @ApiMethod 注释中声明的“路径”和“名称”的值组成的“路径”参数...

    如果它不适用于某些端点,那是我的错误。不过不知道是哪个错误。

    请注意,我注意到将 App Engine Endpoints 的正确 httpMethod 传递给 JavaScript 请求方法非常重要。默认情况下,请求方法假定它是一个 GET。如果您的 Endpoint 在 @ApiMethod 注释中有 httpMethod= ApiMethod.HttpMethod.POST ,您应该传递参数 'method': 'POST',详见文档:https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequestargs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多