C# 如何使用.NET Facebook SDK获取Facebook墙上的链接?
我正在使用这里找到的SDK。。。C# 如何使用.NET Facebook SDK获取Facebook墙上的链接?,c#,asp.net,c#-4.0,facebook-c#-sdk,C#,Asp.net,C# 4.0,Facebook C# Sdk,我正在使用这里找到的SDK。。。 在这一点上,文档似乎严重缺乏。经过一段时间的搜索和实验,我将其拼凑起来 首先,您需要在上设置一个应用程序,以检索您的AppSecret和AppId 然后你需要找到你的用户ID。它是访问{username}时的id字段 事实证明,挑战之一是AccessToken过期。因此,您还必须编写代码,在前一个AccessToken过期时检索新的AccessToken 这就是我想到的 public static class FacebookUpdates { p
在这一点上,文档似乎严重缺乏。经过一段时间的搜索和实验,我将其拼凑起来
- 首先,您需要在上设置一个应用程序,以检索您的AppSecret和AppId
- 然后你需要找到你的用户ID。它是访问{username}时的id字段
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;
}
}
现在,您可以创建一个用户控件,该控件在codebehind中调用GetLinks()
(很抱歉,这个示例是在VB中)
然后用户控件可能看起来像这样
<%@ 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%>
如果您没有编写ASP.NET应用程序或类似程序,则无需编写代码。尝试只使用Windows PowerShell和
<%@ 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%>