如何在承诺模式下使用 JavaScript 库调用 App Engine 端点
How to call App Engine Endpoints with the JavaScript library in promises mode
我有一个 Web 应用程序,它使用 Google API JavaScript 客户端库调用多个 App Engine 端点。
我目前正在按照 Google (https://developers.google.com/api-client-library/javascript/features/promises#using-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 注释中声明的 "path" 和 "name" 的值来工作。
即对于上述端点,以下路径有效:
https://myappenginename.appspot.com/_ah/api/customerApi/v1/customerByParent/listByParent
奇怪的是,这对同类的其他一些端点不起作用。我收到 404 HTTP 错误或 503 错误。
当您使用 APIs Explorer 查询端点但没有成功时,我也尝试使用 "Request" 下显示的路径....
是否有关于如何使用 Google API JavaScript 客户端库调用 App Engine 端点的详细文档?我还没有找到任何。您有什么建议可以分享吗?
提前致谢
实际上,请求方法始终使用 "path" 参数工作,该参数由 @ApiMethod 注释中声明的 "path" 和 "name" 的值组成...
如果它对某些端点不起作用,那是我的错误。但是不知道哪个错误。
请注意,我注意到将 App Engine 端点的正确 httpMethod 传递给 JavaScript 请求方法非常重要。默认情况下,请求方法假定它是 GET。如果您的端点在 @ApiMethod 注释中具有 httpMethod= ApiMethod.HttpMethod.POST,您应传递参数 'method':'POST',如文档中所述:https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequestargs
我有一个 Web 应用程序,它使用 Google API JavaScript 客户端库调用多个 App Engine 端点。
我目前正在按照 Google (https://developers.google.com/api-client-library/javascript/features/promises#using-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 注释中声明的 "path" 和 "name" 的值来工作。
即对于上述端点,以下路径有效: https://myappenginename.appspot.com/_ah/api/customerApi/v1/customerByParent/listByParent
奇怪的是,这对同类的其他一些端点不起作用。我收到 404 HTTP 错误或 503 错误。
当您使用 APIs Explorer 查询端点但没有成功时,我也尝试使用 "Request" 下显示的路径....
是否有关于如何使用 Google API JavaScript 客户端库调用 App Engine 端点的详细文档?我还没有找到任何。您有什么建议可以分享吗?
提前致谢
实际上,请求方法始终使用 "path" 参数工作,该参数由 @ApiMethod 注释中声明的 "path" 和 "name" 的值组成...
如果它对某些端点不起作用,那是我的错误。但是不知道哪个错误。
请注意,我注意到将 App Engine 端点的正确 httpMethod 传递给 JavaScript 请求方法非常重要。默认情况下,请求方法假定它是 GET。如果您的端点在 @ApiMethod 注释中具有 httpMethod= ApiMethod.HttpMethod.POST,您应传递参数 'method':'POST',如文档中所述:https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequestargs