我正在尝试 Riot Games API。我确信我的 URL 没问题,我必须包含的 API key 也没问题;我的请求没有理由返回 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();
}
}
我的进口已全部计算在内。
最佳答案
我查看了您调用 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>
如果上述情况并非如此,并且您实际上正确地传递了 key 并且仍然得到
{"status": {"message": "Access denied", "status_code": 401}}
响应,那么 key 很可能是无效的。
关于java - 使用 Java 检索 Riot API 信息时出现 401(未经授权),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20591872/