【问题标题】:Getting a 401 (Unauthorized) while using Java to retrieve Riot API information使用 Java 检索 Riot API 信息时出现 401(未经授权)
【发布时间】:2013-12-15 06:27:05
【问题描述】:

我正在试验 Riot Games API。我确定我的 URL 很好,我必须包含的 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();
        }

    }

我的进口都算进去了。

【问题讨论】:

  • 您确定不需要包含一些身份验证 http 标头,例如基本身份验证之类的吗?
  • 不确定它是否也可以这样工作;您可以尝试使用其中一种 java riot-api 包装器

标签: java json api riot-games-api


【解决方案1】:

我查看了您用来调用 API 的 URL。我不确定您是否在此处输入错误:

https://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick&amp;api_key=&lt;key&gt;

您需要将 api_key 作为参数传递,因此请在 ? 之前使用 ? 而不是 &amp; ,即如下所示:

https://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key=&lt;key&gt;

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

【讨论】:

  • 你说得对,这行得通。不知道“&”是如何出现在那里的。非常感谢。
猜你喜欢
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 2013-09-24
  • 2014-10-09
相关资源
最近更新 更多