【问题标题】:Parse REST API, Validate User session解析 REST API,验证用户会话
【发布时间】:2015-09-23 14:55:50
【问题描述】:

我正在为游戏使用 Parse REST API,尽管有用于 Parse 的 java 库我想使用 java.net 处理我自己的传输协议以用于学习目的。请远离诸如为什么我不使用 Apache HttpClient 之类的东西。

遵循 Parse REST API 指南

这是我想要达到的目标:


前两步工作正常,前者使用 POST 请求方法,后者使用带有一些参数的 GET。

牢记Request and Response 格式,我还提供了Application-IDREST-API-Key,它们是所需的适当请求标头。

现在,对于使用不带参数但带有附加标头的 GET 请求的第三步,API 期望提供 Session-Token


代码

private static void validateSessionToken(String token) throws IOException {
    System.out.println("Token: " + token);

    URL url = new URL("https://api.parse.com/1/users/me");
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("X-Parse-Application-Id", "xxxxxxx");
    con.setRequestProperty("X-Parse-REST-API-Key", "xxxxxxxx");
    con.setRequestProperty("X-Parse-Session-Token", token);
    con.setRequestProperty("content-type", "application/json");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    if(responseCode == 400) {
        System.out.println("Bad request!");
        return;
    }

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    System.out.println(response.toString());
}

输出

Sending 'GET' request to URL : https://api.parse.com/1/users/me
Response Code : 400
Bad request!

调试

我一直在使用 PARSE API CONSOLE 和 Chrome 网络调试工具来尝试看看有什么区别,但看不到任何东西。

来自维基:

400 错误请求

由于某些被认为是客户端错误(例如,格式错误的请求语法、无效的请求消息帧或欺骗性请求路由),服务器无法或不会处理请求

一些可能有帮助的照片

【问题讨论】:

    标签: java rest session token


    【解决方案1】:

    所以当我向我的 JsonObject 询问会话令牌时:

    jsonObject.get("sessionToken").toString();
    

    它返回:"r:WzB7qdmhkcW5qd2moM8gbBLDp" 引用,但使用:

    jsonObject.get("sessionToken").getAsString();
    

    它返回:r:WzB7qdmhkcW5qd2moM8gbBLDp

    我太专注于请求,我什至没有注意到......

    【讨论】:

      猜你喜欢
      • 2015-08-26
      • 2019-02-20
      • 1970-01-01
      • 2017-05-30
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      相关资源
      最近更新 更多