需要auth_key:Quickblox错误

时间:2017-04-11 13:21:09

标签: java rest quickblox

我正在使用Quickblox REST API。当我试图从POSTMAN运行时,它给了我正确的输出。但是,当我通过我的java代码使用它时。它向我显示错误:

auth_key is required

这是我的java代码:

URL url = new URL("https://api.quickblox.com/session.json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");

String current_date = dateFormat.format(cal.getTime());
Date newdt = dateFormat.parse(current_date);
long unixTime = newdt.getTime() / 1000;
String nonce = randomString(5);
String message ="application_id=XXXXX&auth_key=XXXXX&nonce=xxxxtimestamp=xxxx";
JSONObject arrayElement = new JSONObject();
arrayElement.put("application_id", "xxxxxx");
arrayElement.put("auth_key", "xxxxxxx");
arrayElement.put("nonce", nonce);
arrayElement.put("timestamp", unixTime);
arrayElement.put("signature", hmacDigest(message, secret, "HmacSHA1"));
conn.setRequestProperty("data", arrayElement.toJSONString());
conn.connect();

BufferedReader in = new BufferedReader(new   InputStreamReader(conn.getInputStream()));
String inputLine;
HashMap hmdata = new HashMap();
while ((inputLine = in.readLine()) != null) {
      hmdata.put("data", inputLine);
   }
in.close();

有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

如果您以json身份发送,请添加以下标头:

conn.setRequestProperty("Content-Type", "application/json");

因为服务器不了解您发送请求的有效负载的格式