简体   繁体   中英

How can I get the links on my Facebook wall with the .NET Facebook SDK?

I'm using the SDK found here... http://facebooksdk.net/

And the documentation seems to be severely lacking at this point.

After searching around awhile and experimenting I pieced this together.

It turns out that one of the challenges is that AccessTokens expire. So you also have to write code that will retrieve a new AccessToken when the previous one expires.

And this is what I came up with...

public static class FacebookUpdates
{
    private static string AppId = "00000000";
    private static string AppSecret = "xxxxxxxxx";
    private static string UserID = "0000000000";
    private static string AccessToken;

    public static dynamic GetLinks()
    {
        var fb = new FacebookClient();
        if (string.IsNullOrEmpty(AccessToken)) AccessToken = GetAccessToken(fb); // Cache the result in static variable AccessToken
        fb.AccessToken = AccessToken;

        var query = string.Format("SELECT link_id, title, url FROM link WHERE owner = {0} LIMIT 5", UserID);

        dynamic parameters = new ExpandoObject();
        parameters.q = query;
        dynamic results;

        try
        {
            results = fb.Get("/fql", parameters);
        }
        catch (FacebookOAuthException)
        {                
            AccessToken = GetAccessToken(fb); // Cache the result in static variable AccessToken
            fb.AccessToken = AccessToken;

            // Retry with new AccessToken
            results = fb.Get("/fql", parameters);
        }

        return results;
    }

    private static string GetAccessToken(FacebookClient fb)
    {
        dynamic result = fb.Get("oauth/access_token", new
        {
            client_id = AppId,
            client_secret = AppSecret,
            grant_type = "client_credentials"
        });
        return result.access_token;
    }
}

Now you can create a user control that calls GetLinks() in the codebehind (sorry this example is in VB)...

Public Shared FacebookLinks As Object = FacebookUpdates.GetLinks().data

And then the user control might look like this...

<%@ OutputCache Duration="600" VaryByParam="None" %>
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="FacebookUpdates.ascx.vb" Inherits="FacebookUpdates" %>
<%For Each Link In FacebookLinks%>
    <p><a href="<%= Link.url %>" rel="nofollow" target="_blank"><%= Link.title%></a></p>
<%Next%>

If you're not writing an ASP.NET application or the like, you don't need to write code. Try just using Windows PowerShell and http://facebookpsmodule.codeplex.com .

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