2013-12-15 38 views
1

我正在試驗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(); 
     } 

    } 

我的進口都佔了。

+0

你確定你不需要包含一些身份驗證http頭像基本身份驗證或什麼? – radai

+0

不確定它是否也可以這樣工作;你可以嘗試使用其中一個java riot-api包裝 – AscendedKitten

回答

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}}效應初探,那麼關鍵我這很可能是無效的。

+0

你是對的,那很有效。不確定'&'如何在那裏結束。非常感謝。 – bagochips44