0

I've been trying the new authentication system of Minecraft API but i've got some problems!

I've got this code:

    $(function() {
    $(" #submit ").click(function() {
        $("#res").remove();
        $("#loading").remove();
        $("body").append('<span id="loading">Loading...</span>');

        var user = $("#user").val();
        var pass = $("#pass").val();

        $.ajax({
            url:'https://authserver.mojang.com/authenticate',
            type: 'POST',
            contentType: 'application/json',
            data: '{"agent": {"name": "Minecraft", "version": 1}, "username": "' + user +'", "password": "' + pass +'"}',
            dataType: "jsonp",
            success: function(res) {
                $("#loading").remove();
                $("#res").remove();
                $("body").append('<div id="res">' + res + '</div>');
            }
            error: function() {
                $("#loading").remove();
                $("#res").remove();
                $("body").append('<div id="res">Error</div>');
            }
        });         
        return false;
    });
});

It does not work for me, can you help please?

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
demetriomontalto
  • 111
  • 1
  • 1
  • 11
  • Can you show us what error you are getting? – Lewis Norton Mar 19 '15 at 14:42
  • Can't POST jsonp. Try json – scniro Mar 19 '15 at 14:44
  • Use parameters with error callback error:function(jqXHR, textStatus, errorThrown) { } and show us what is returned in errorThrown parameter. – SSA Mar 19 '15 at 14:45
  • With json I get this error: XMLHttpRequest cannot load https://authserver.mojang.com/authenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405. With jsonp something like: GET -> and then the link with user=&pass= etc. – demetriomontalto Mar 19 '15 at 14:58
  • Try dataType: "json", as @sal niro already said. – SSA Mar 19 '15 at 15:01
  • I tried and I reported the error above: "XMLHttpRequest cannot..." – demetriomontalto Mar 19 '15 at 15:02
  • http://stackoverflow.com/a/13576511/1464112 – scniro Mar 19 '15 at 15:09
  • @salniro that is the old auth method, this is a new one that you can do by json and no more from the link – demetriomontalto Mar 19 '15 at 15:12
  • I'm not an expert in cross origin AJAX requests* but with some fiddling I got some success using `curl`. So another approach might be utilising your server and performing the requests there i nstead inside client's browser? (* My chromium seemed to ate the POST type which wasn't well received by mojang - something @salniro has pointed out) – nuala Mar 19 '15 at 16:35

1 Answers1

0

This is a cross origin browser issue. As pointed out in the comments you may need to delegate additional resources to handle these requests. Check it out with Fiddler or Postman. Getting a good response, 403, clearly, wrong creds.. but to demonstrate...

Raw Post Request (paste into Fiddler raw composer)

POST https://authserver.mojang.com/authenticate HTTP/1.1
Host: authserver.mojang.com
Content-Length: 86

{ "agent": { "name": "Minecraft", "version": 1 }, "username": "US", "password": "PW" }

Response

{"error":"ForbiddenOperationException","errorMessage":"Invalid credentials. Invalid username or password."}

scniro
  • 16,844
  • 8
  • 62
  • 106
  • Could you help? I don't even know where to start, I only know standard request. What I have to add? – demetriomontalto Mar 19 '15 at 18:01
  • Assuming you have a server side back end, issue a request there. When you get that request, transform it with whatever stack you are using to re-issue the request. Get the response, return back to your client. Just an extra step. Not sure what technologies you are using, but there should be api's available for whatever route you take. – scniro Mar 19 '15 at 18:04
  • Here there are more infos http://wiki.vg/Authentication but this seems the only way – demetriomontalto Mar 19 '15 at 18:11
  • Did you issue the request through Fiddler like I pointed out? Think of Fiddler as the server you'll need to create. Issue your ajax js call to your server, which you'll relay to mojang => return to client. – scniro Mar 19 '15 at 18:16
  • I just downloaded Fiddler, could you help me on tw or similiar? – demetriomontalto Mar 19 '15 at 18:21
  • just fire it up => composer => post => https://authserver.mojang.com/authenticate => paste json in request body => execute (looks for response on left hand side) – scniro Mar 19 '15 at 18:46
  • try https:// I verified this working, try again. Paste the json snippet in my answer – scniro Mar 19 '15 at 18:54
  • Edited my request, paste the exact text with the exact spacing into Fiddler raw composer – scniro Mar 19 '15 at 19:03
  • Build a backed and proxy the request. This is just to demo your request can't be completed from the browser. You need to develop additional infrastructure to do this. Many ways this can be done, really no quick answer – scniro Mar 19 '15 at 19:24
  • I'm not really prepared about this, could you please help with some code? – demetriomontalto Mar 19 '15 at 19:30