我正在試驗Riot Games API。我知道我的URL很好,我必須包含的API密鑰也很好;沒有理由說我的請求應該返回401.起初我認爲它與Eclipse有關,但將API放入瀏覽器中也會返回401(API通常返回的JSON格式)。我知道有論壇,這一點,但我想,也許,如果有我的程序中的錯誤這裏有人能指出:使用Java檢索Riot API信息時獲取401(未授權)
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頭像基本身份驗證或什麼? – radai
不確定它是否也可以這樣工作;你可以嘗試使用其中一個java riot-api包裝 – AscendedKitten