简体   繁体   中英

Getting a 401 (Unauthorized) while using Java to retrieve Riot API information

I'm experimenting with the Riot Games API. I know for sure that my URL is fine, my API key that I have to include is also fine; there is no reason that my request should return a 401. At first I thought it had to do with Eclipse, but putting the API in my browser also returns a 401 (in the JSON format that the API usually returns). I know there are forums for this, but I thought maybe someone here could point out if there was an error in my program:

public class APITry {
    public static void main(String[] args) throws IOException{

        URL LOLAPI = new URL("https://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick&api_key=<key>"); //<key> is, of course, replaced with my key
        BufferedReader in = new BufferedReader(new InputStreamReader(LOLAPI.openStream()));
        StringBuilder build = new StringBuilder();

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            build.append(in.toString());
        in.close();

        String INFO = build.toString();

        JSONParser prse = new JSONParser();

        try {
            Object obj = prse.parse(in);
            JSONObject PARSED = (JSONObject) obj;
            String message = (String) PARSED.get("message");
            int summonerID = (int) PARSED.get("losses");
            System.out.println("message:" + message);
            System.out.println("retrieved, is " + summonerID);
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("parse exception");
        } catch(NullPointerException e) {
            e.printStackTrace();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        }

    }

My imports are all accounted for.

I looked at the URL that you are invoking the API with. I am not sure if you have made a typo while putting it here:

https://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick&api_key=<key>

You need to pass in the api_key as a parameter, so instead of & , use a ? before the api_key ie like the following:

https://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key=<key>

If the above is not the case and you are in fact passing the key correctly and still getting a {"status": {"message": "Access denied", "status_code": 401}} reponse, then the key is most likely invalid.

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