在使用Java检索Riot API信息时获取401(未授权)

时间:2013-12-15 06:27:05

标签: java json api riot-games-api

我正在尝试使用Riot Games API。我确定我的网址很好,我必须包含的API密钥也很好;没有理由我的请求应该返回401.起初我认为它与Eclipse有关,但是在我的浏览器中放置API也会返回401(API通常返回的JSON格式)。我知道有这样的论坛,但我想也许有人可以指出我的程序中是否有错误:

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

    }

我的进口全部都计入了。

1 个答案:

答案 0 :(得分:0)

我查看了您正在调用API的URL。我不确定你是否在把它放在这里时输了一个错字:

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

您需要传递api_key作为参数,而不是&,而是在?之前使用api_key,例如:

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

如果上述情况并非如此,并且您实际上正确地传递了密钥并仍然获得了密钥 {"status": {"message": "Access denied", "status_code": 401}}响应,那么密钥很可能无效。