【问题标题】:Unable to test Google Cloud Endpoints on Java Server from Postman无法从 Postman 测试 Java 服务器上的 Google Cloud Endpoints
【发布时间】:2016-09-19 16:26:45
【问题描述】:

我将此存储库部署到 Google App Engine,它是 Google Cloud Endpoints Java 服务器的一个示例。 https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-java

该应用运行良好。我能够使用 Google API Explorer 测试 REST API 调用。但是,我想使用 Postman(一个 Chrome 扩展,它是一个 REST 客户端)等外部工具来测试对这个应用程序的这些 REST API 调用。

我已成功从 Google 检索访问令牌。但是当我从 Postman 或普通浏览器拨打电话时,该应用程序不断拒绝 API 调用并出现“无效用户”错误。我尝试记录返回 null 的输入用户。

下面的 sn-p 是我试图访问的 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 应用程序集成。最好不要使用 Google Cloud Endpoints 的客户端部分,而是使用 Oauth 2.0 的手动处理来最大程度地减少代码重组。

【问题讨论】:

    标签: java rest google-app-engine oauth google-cloud-endpoints


    【解决方案1】:

    我没有为此使用 Postman 内置的 OAuth 2.0 授权,但如果您将 Authorization 请求标头设置为检索到的访问令牌,它应该可以正常工作。

    键:

    Authorization
    

    价值:

    Bearer access_token
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2020-02-24
      • 2020-03-22
      • 2018-09-23
      • 2019-10-27
      • 1970-01-01
      • 2014-04-19
      相关资源
      最近更新 更多