Facebook Graph API Get Access Token via JQuery (Client way)

OK, so this is driving me nuts. Have been trying to get the access token for my app using jquery. When I have the access token everyting is working fine. I can retrieve friends list etc. If I sniff the URL from my request and paste it in a browser it does as expected (gives me the access token and redirects to my page).

But...using the below function either results in a "jQuery15105307047630302168_1315247312743 was not called" or if I change the dataType to "JSON" in a "No transport" error message. I have been reading a bunch of posts and nothing seems to do the trick.

Some suggest adding ?callback? and a parameter which is the callback function itself, but this gives me the exact same error. Below is the function that I am calling to get the access token.

function GetAccessTokenClient() {

var redirURL = "http://banana.ihedge.dk/bet/";
var scope = "user_online_presence"; // user_photos, email, user_birthday, user_online_presence
var clientID = "105719222863801";

try {
    $.ajax({
        type: "GET",
        url: "https://www.facebook.com/dialog/oauth", //"https://graph.facebook.com/oauth/authorize",
        cache: false,,
        dataType: 'JSONP',
        data: "client_id=" + clientID + "&redirect_uri=" + redirURL + "&reponse_type=token",
        error: function(xhr, status, error) {
            alert(error);
        },
        success: function(response) {
            for (key in repsonse)
                alert(key);
        },
        complete: function(request, textStatus) {
            //alert('RESPONSETEXT: ' + request.responseText);
            //alert('COMPLETE: ' + textStatus);
        }
    });
}
catch (Error) {
    alert(Error);
}
}

Please help.

Upvotes: 1

Views: 5629

Answers (1)

Jont
Jont

Reputation: 972

The problem is with trying to get https://www.facebook.com/dialog/oauth using ajax, it's not designed for that im afraid.

The simplest way to get the users access token using javascript/jquery is to use FB.Login

FB.login(function(response) {
    if (response.authResponse) {
        var accessToken = response.authResponse.accessToken;
    }
});

Upvotes: 2

Related Questions