简体   繁体   中英

Getting 401 error on REST API on parse.com(Java)

I am using parses REST API to get a version ID for my program and when i am trying to connect the java program returns IOException with 401 error but when i connect to that with the browser i get the JSON format correctly why?

I am getting this error.

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)

`

My program code is :

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");
    }

Try setting the custom headers 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());
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM