简体   繁体   中英

Finding facebook user identifier, who liked a page

在此处输入图片说明

在此处输入图片说明

I have facebook like button on my webpage. When someone clicks to like it, then is there any way to know who liked it, any facebook user identifier?

Also, if someone has already liked that and coming back to my page, then is there anyway to know that the visitor has already liked it and what is that visitor's facebook unique identifier?

Pl. assume, user is already logged into facebook. I read about FB.Event.subscribe at https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe but that just tells me that someone liked my page, but there is no way to know "WHO"

I have flexibility to change the button to link or anything else, as long as I can get unique identifier.

Something I built couple years ago so not sure if it's still working considering Facebook changes it's API frequently. Maybe you can tweak this a little or maybe it gives you some new ideas of how to retrieve the userId:

var pageID = "@Configuration.FbPageID",
    appID = "@FacebookApplication.Current.AppId",
    returnURL = "@Configuration.FbReturnURL";
    OAuthResponse = {},
    fbReturnHandler = function () {
        if (!$.isEmptyObject(response)) {
            OAuthResponse = response.authResponse;
            isFan();
        }
    },
    isFan = function () {
        if (!$.isEmptyObject(OAuthResponse.accessToken)) {
            FB.api("me/likes/" + pageID + "?access_token=" + OAuthResponse.accessToken, function (r) {
                if (r.data.length) {
                    window.location = "/@ViewBag.Language/Home/step2?uid=" + OAuthResponse.userID;
                }
            });
        }
    };

window.fbAsyncInit = function () {
    FB.init({ appId: appID, status: true, cookie: true, xfbml: true, oauth: true });

    FB.Event.subscribe('edge.create', function (response) {
        if (!$.isEmptyObject(response)) {
            OAuthResponse = response.authResponse;
            window.location = "/@ViewBag.Language/Home/step2?uid=" + OAuthResponse.userID;
        }
    });

    if ($.isEmptyObject(OAuthResponse)) {
        FB.ui({
            client_id: appID,
            method: 'oauth',
            scope: 'user_likes',
            redirect_uri: returnURL,
            response_type: 'token'
        });
    } else {
        isFan();
    }
};

The returnURL is a dummy page which handles the OAuth request and returns the value of the response to your application. Since it doesn't automatically close, you can work around it by adding a small script:

<script type="text/javascript">
    if (window.opener) {
        window.opener.fbReturnHandler();
        window.close();
    }
</script>

The like button is in XFBML:

<span class="fb-like"><fb:like href="http://www.facebook.com/yourfanpage" send="false" width="49" show_faces="false" font="arial"></fb:like></span>

Apparently this can be HTML5 these days: generate here .

As stated in the comments, to get a userId you'll have to go through some facebook policy (scope) to allow the user to share this information with your application. Therefor it's required to build your own application on facebook.

After your application is setup, I found the Graph API Explorer to be very helpful in getting the proper pageId and appId . Don't get confused by the ID in the URL when browsing FB apps, fanpages ...

The OAuthResponse object fetches the access token, required for your application to allow sharing information. Once succesful, you'll find the userId in response.authResponse.userID .

Similar information can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM