403响应代码 - 输入是否正确?

时间:2018-06-03 05:59:02

标签: java url

不确定你们中有多少人熟悉Bukkit / Mojang API,但是,这就是我今天正在使用的。虽然我不相信这会与问题有任何关联,因为它似乎是一个本土问题。

我在Mojang的身份验证维基之后连接到身份验证服务: http://wiki.vg/Authentication#Authenticate

在我的代码中,' MinecraftAccount'只是一个表示上述链接中有效负载的对象,我使用GSON进行序列化。

然后我想获取发回的输入响应,您可以在上面的链接中找到属性。我使用另一个对象来保存json值并使用Json(AuthResponse)对其进行反序列化。

看来我正在做的一切正确,但我得到403错误。我不确定为什么,因为经过几次尝试才能做到这一点,它起作用了,然后又自发地开始吐出403。希望你们能在这里看到这个问题。

public String getAuthToken(MinecraftAccount minecraftAccount) {
    try {
        HttpURLConnection httpURLConnection = Webs.getConnection("https://authserver.mojang.com/authenticate");
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setInstanceFollowRedirects(false);
        httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        httpURLConnection.setUseCaches(false);

        httpURLConnection.addRequestProperty("User-Agent", "Mozilla/4.0");

        IOUtils.write(GsonProvider.standard().toJson(minecraftAccount), httpURLConnection.getOutputStream(), StandardCharsets.UTF_8);
        return GsonProvider.standard().fromJson(IOUtils.toString(httpURLConnection.getInputStream(), StandardCharsets.UTF_8), AuthResponse.class).getAccessToken();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
    return null;
}

    public void uploadSkin(UUID uuid, String url, boolean normal) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(ImageIO.read(new URL(url)), "png", baos);

        String authenticityToken = this.getAuthToken(this.minecraftAccount);

        try {
            System.out.println("Token: " + authenticityToken);

            if (authenticityToken == null) {
                return;
            }

            HttpURLConnection httpURLConnection = Webs.getConnection(String.format(UPLOAD_SKIN_URL, UUIDTypeAdapter.fromUUID(uuid)));
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);

            httpURLConnection.addRequestProperty("Authorization", "Bearer " + authenticityToken);
            try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), StandardCharsets.UTF_8))) {
                writer.write("model=");
                if (!normal) {
                    writer.write("slim");
                }

                writer.write("&url=" + UrlEscapers.urlPathSegmentEscaper().escape(url));
                System.out.println("status: " + httpURLConnection.getResponseMessage() + "(" + httpURLConnection.getResponseCode() + ")");
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

Web#getConnection就是这样(使用Proxy.NO_PROXY):

    public static HttpURLConnection getConnection(String url, Proxy proxy) throws IOException {
    HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection(proxy);

    httpConnection.setConnectTimeout(TIMEOUT);
    httpConnection.setReadTimeout(2 * TIMEOUT);

    httpConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json");
    httpConnection.setRequestProperty(HttpHeaders.USER_AGENT, "Mozilla/5.0");
    return httpConnection;
}

PS:是的,我已经查看了其他几个403线程,但是,他们的解决方案(例如将用户代理设置为修复)并不起作用。

最终,调用#uploadSkin时没有错误,它只返回403并且实际上并没有将文件上传到网站或其他任何内容。该方法只输出:

Output

0 个答案:

没有答案