2016-05-30 2 views
1

私は考えているすべてを試したか、またはgoogleで検索して見つけましたが、私が試したものは何でも試してみました。 405または悪い要求...Minecraft認証サーバーが返す405

ここで私の最新の試みは405を返します|方法が許可されていません:

public class Main { 
static String authServer = "https://authserver.mojang.com"; 

public static void main(String[] args) throws Exception { 
    auth(); 
} 
//{"agent": { "name": "Minecraft", "version": 1 }, "username": "example", "password": "password"} 


static void auth() throws IOException { 
    URL url = new URL(authServer); 
     HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
     httpCon.setDoOutput(true); 
     httpCon.setRequestMethod("POST"); 

     OutputStreamWriter out = new OutputStreamWriter(
     httpCon.getOutputStream()); 
     System.out.println(httpCon.getResponseCode()); 
     System.out.println(httpCon.getResponseMessage()); 
     out.close(); 

} 

} 

答えて

1

すぐにhttps://authserver.mojang.comに接続しようとしています。これが認証に使用するサイトですが、正しいページではありません。必要な特定のタスクにエンドポイントを使用する必要があります。認証の場合は、authenticate endpoint/authenticateを使用します。

これは、使用する必要があるURLがではなく、https://authserver.mojang.com/authenticateであることを意味します。

ご要望を承諾するためにContent-Typeapplication/jsonに設定する必要があります。

the errors documentationの場合、間違った方法で間違ったターゲットを使用した場合にのみ、Method Not Allowedが得られます。あなたはその場合にはNot Foundを得るだろうと期待していますが、実際にコードをそのままテストしていないので、実際には許可されていないメソッドが生成される可能性があります。


はここJamesst20によってthis answerのオフに基づいて認証する方法のサンプルです:

private static String authenticateEndpoint = "https://authserver.mojang.com/authenticate"; 

public static void main(String[] args) throws Exception { 
    auth("{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"example\", \"password\": \"password\"}"); 
} 

private static String auth(String data) throws Exception { 
    URL url = new URL(authenticateEndpoint); 

    byte[] contentBytes = data.getBytes("UTF-8"); 

    HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setRequestProperty("Accept-Charset", "UTF-8"); 
    connection.setRequestProperty("Content-Type", "application/json"); 
    connection.setRequestProperty("Content-Length", Integer.toString(contentBytes.length)); 

    OutputStream requestStream = connection.getOutputStream(); 
    requestStream.write(contentBytes, 0, contentBytes.length); 
    requestStream.close(); 

    String response; 
    BufferedReader responseStream; 
    if (connection.getResponseCode() == 200) { 
     responseStream = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); 
    } else { 
     responseStream = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "UTF-8")); 
    } 

    response = responseStream.readLine(); 
    responseStream.close(); 

    if (connection.getResponseCode() == 200) { 
     return response; 
    } else { 
     // Failed to log in; response will contain data about why 
     System.err.println(response); 
     return null; 
    } 
} 
+0

はありがとう:D、私はすでに(..もうそこにそのありえない)のコメントを読んで、エンドポイントを変更し、要求されていない/サポートされていないメディアの種類があります。私はあなたのコードを後で試してみるつもりです。 – Clyme

+0

大丈夫、それは働いた:Dありがとう – Clyme

関連する問題