【问题标题】:Minecraft Authentication Server Returns 405Minecraft 身份验证服务器返回 405
【发布时间】:2016-05-30 19:30:24
【问题描述】:

我已经尝试验证我的世界帐户有一段时间了,我已经尝试了所有我能想到的或通过谷歌搜索找到的所有内容。但无论我尝试什么,我都会收到各种不同的错误,例如 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();

 }

}

【问题讨论】:

    标签: java http authentication minecraft


    【解决方案1】:

    现在,您正在尝试连接到https://authserver.mojang.com。虽然这是您用来进行身份验证的网站,但它不是正确的页面。您需要将端点用于所需的特定任务。对于身份验证,您要使用authenticate endpoint/authenticate

    也就是说你需要使用的网址是https://authserver.mojang.com/authenticate,而不仅仅是https://authserver.mojang.com

    您还需要将Content-Type 设置为application/json 才能接受您的请求。

    根据the errors documentation,只有当您使用错误的方法而不是错误的目标时,您才会得到Method Not Allowed。我希望在这种情况下你会得到Not Found,但我还没有完全测试你的代码,所以它实际上可能会产生不允许的方法。


    以下是如何根据this answer by Jamesst20 进行身份验证的示例:

    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,我已经阅读了评论(不再存在..)并更改了端点并获得了错误的请求/不受支持的媒体类型。我稍后会试试你的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2012-04-25
    • 2015-03-23
    • 2014-11-25
    相关资源
    最近更新 更多