![](/img/trans.png)
[英]Migrating to Google Cloud Endpoints Framework for Java
[英]Unable to test Google Cloud Endpoints on Java Server from Postman
我将此仓库部署到了Google App Engine,这是Google Cloud Endpoints Java服务器的一个示例。 https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-java
该应用程序运行良好。 我能够使用Google API Explorer测试REST API调用。 但是,我想使用外部工具(例如Postman(作为REST客户端的Chrome扩展程序)等)测试对此应用程序的这些REST API调用。
我已成功从Google检索访问令牌。 但是,当我从Postman或普通浏览器拨打电话时,该应用程序始终拒绝API调用,并显示“无效用户”错误。 我尝试记录返回null的输入User。
下面的代码片段是我试图访问的REST API。 该方法的参数User是从GoogleAppEngineAuthenticator检索的,我不知道它是如何工作的。
/**
* Provides the ability to query for a collection of Score entities.
*
* @param limit
* maximum number of entries to return
* @param order
* how the entries should be ordered
* @param user
* object representing the current user making requests
* @return the collection of Score entities
* @throws OAuthRequestException
* if the token included in the request is invalid, the client
* ID included in the token is not in the list of allowed
* clientIds, or the audience included in the token is not in
* the list of allowed audiences.
* @throws IOException
*/
@ApiMethod(name = "scores.list")
@SuppressWarnings("unchecked")
public List<Score> list(@Nullable @Named("limit") String limit, @Nullable @Named("order") String order, User user)
throws OAuthRequestException, IOException {
System.out.println(user);
PersistenceManager pm = getPersistenceManager();
Query query = pm.newQuery(Score.class);
if (order != null) {
if (order.equals(WHEN)) {
query.setOrdering("played desc");
} else if (order.equals(OUTCOME)) {
query.setOrdering("outcome asc");
}
} else {
query.setOrdering("played desc");
}
if (user != null) {
query.setFilter("player == userParam");
query.declareParameters("com.google.appengine.api.users.User userParam");
} else {
throw new OAuthRequestException("Invalid user.");
}
if (limit == null) {
limit = DEFAULT_LIMIT;
}
query.setRange(0, new Long(limit));
return (List<Score>) pm.newQuery(query).execute(user);
}
之所以尝试从外部进行测试,是因为我需要现有的移动应用程序才能与此Google Cloud App集成。 最好,我将尽量不使用Google Cloud Endpoints的客户端部分,而是使用Oauth 2.0的手动处理来最大程度地减少代码重组。
我没有为此目的使用Postman内置的OAuth 2.0授权,但是如果将Authorization request标头设置为检索到的访问令牌,则它应该可以正常工作。
键:
Authorization
值:
Bearer access_token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.