すぐにhttps://authserver.mojang.com
に接続しようとしています。これが認証に使用するサイトですが、正しいページではありません。必要な特定のタスクにエンドポイントを使用する必要があります。認証の場合は、authenticate endpoint:/authenticate
を使用します。
これは、使用する必要があるURLがではなく、https://authserver.mojang.com/authenticate
であることを意味します。
ご要望を承諾するためにContent-Type
〜application/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;
}
}
はありがとう:D、私はすでに(..もうそこにそのありえない)のコメントを読んで、エンドポイントを変更し、要求されていない/サポートされていないメディアの種類があります。私はあなたのコードを後で試してみるつもりです。 – Clyme
大丈夫、それは働いた:Dありがとう – Clyme