How to set Facebook access (from server) in Javascript SDK

If I use a server-side thread to authenticate a user and get an access token, how can I set this access token in the JavaScript SDK and use it to make calls from the client side?

+5
source share
1 answer

As soon as the user completes the authentication flow on the server side, the user is already authorized for your application, all you need to do is use FB.getLoginStatus :

FB.getLoginStatus(function(response) {
    if (response.status === "connected") {
        console.log("authResponse: ", response.authResponse);
        FB.api("me", function(response2) {
            console.log("Hey there " + response2.name);
        });
    }
    else if (response.status === "not_authorized") {
        // user is logged in to facebook but hasn't authorized your app, should not happen if he went through the server side authentication
    }
    else {
        // user is logged out of facebook, also should not happen
    }
}

, js sdk , , , , authResponse :

authResponse: {
    accessToken: "aaaaaaa",
    expiresIn: "bbbbbb",
    signedRequest: "cccccc",
    userID: "dddddd"
}

facebook , getLoginStatus .
, , , .

:

- -, Javascript SDK

FB.getLoginStatus() : true , FB.init() , , Facebook, authResponse, , , .

, .

+4

All Articles