gpt4 book ai didi

java - parse.com 上的 REST API 出现 401 错误(Java)

转载 作者:行者123 更新时间:2023-12-01 14:38:26 25 4
gpt4 key购买 nike

我正在使用解析 REST API 来获取我的程序的版本 ID,当我尝试连接 java 程序时返回 IOException 并显示 401 错误,但是当我使用浏览器连接到该程序时,我正确地获取了 JSON 格式,为什么?

我收到此错误。

java.io.IOException: Server returned HTTP response code: 401 for URL: https://XXXXXX-key=XXXXXXXXX@api.parse.com/1/classes/NicksNoteVersion/xngGDAa1k3
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at startup.Launcher.main(Launcher.java:26)

`

我的程序代码是:

try{
URL url = new URL("https://XXXXXX:javascript-key=XXXXXXX@api.parse.com/1/classes/NicksNoteVersion/xngGDAa1k3");
JSONTokener tokener = new JSONTokener(url.openStream());
JSONObject json = new JSONObject(tokener);
}catch(Exception e){
e.printStackTrace();
System.out.println("Version retrieval failed");
}

最佳答案

尝试设置自定义 header as documented :

Authentication is done via HTTP headers. The X-Parse-Application-Id header identifies which application you are accessing, and the X-Parse-REST-API-Key header authenticates the endpoint.

URL url = new URL("...");
URLConnection conn = url.openConnection();
conn.setRequestProperty("X-Parse-Application-Id", "...");
conn.setRequestProperty("X-Parse-REST-API-Key", "...");
JSONTokener tokener = new JSONTokener(conn.getInputStream());
...

关于java - parse.com 上的 REST API 出现 401 错误(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16249173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com