Skip to main content
 首页 » 编程设计

c#之将消息从网站发布到 Facebook 墙

2024年02月13日13langtianya

我有一个使用 ASP.NET webform .NET 4.5 C# 制作的网站。这个站点包含一个论坛(由我自制),这个论坛的部分内容需要发布到特定的 facebook 墙上(为这个网页制作)。我需要什么:

  1. 将论坛特定部分刚刚创建的话题(消息)发布到相应的 facebook 墙上。

  2. 可选:将网页上的论坛线程与特定 Facebook 页面上的消息/评论同步

我看过这些指南: http://www.codeproject.com/Articles/569920/Publish-a-post-on-Facebook-wall-using-Graph-API http://www.c-sharpcorner.com/UploadFile/raj1979/post-on-facebook-users-wall-using-Asp-Net-C-Sharp/

但我不确定这是否真的适合我?我已尝试按照指南进行操作,但看起来不一样。

编辑:

dynamic result; 
 
        //https://developers.facebook.com/tools/explorer/ 
        //https://developers.facebook.com/tools/access_token/ 
        FacebookClient client = new FacebookClient(ConfigurationManager.AppSettings["FacebookAppToken"]); 
 
        //result = client.Get("debug_token", new 
        //{ 
        //    input_token = ConfigurationManager.AppSettings["FacebookAppToken"], 
        //    access_token = ConfigurationManager.AppSettings["FacebookAppToken"] 
        //}); 
 
 
        //result = client.Get("oauth/access_token", new 
        //    { 
        //        client_id = ConfigurationManager.AppSettings["FacebookAppId"], 
        //        client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"], 
        //        grant_type = "client_credentials", 
        //        //redirect_uri = "http://www.MyDomain.net/", 
        //        //code = "" 
        //    }); 
 
        result = client.Get("oauth/access_token", new 
        { 
            client_id = ConfigurationManager.AppSettings["FacebookAppId"], 
            client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"], 
            grant_type = "client_credentials" 
        }); 
        client.AccessToken = result.access_token; 
 
        result = client.Post("[IdOfFacebookPage]/feed", new { message = "Test Message from app" }); 
        //result.id; 
        result = client.Get("[IdOfFacebookPage]"); 
 
        return false; 

请您参考如下方法:

张贴到 facebook 墙上的方法:

  1. 您需要在 Facebook 开发者工具中注册。
  2. 创建应用(填写表格)。
  3. 下载 FGT 5.0.480(Facebook 图形工具包)
  4. 在您的网络应用程序中引用 FGT dll。
  5. FGT 有发布到 facebook 墙的方法,但需要 appId。
  6. 使用您在第 2 步中创建的应用的应用 ID。

要通过 Facebook 发布到用户的墙上,只能通过应用程序实现。

试试这个 asp .net 应用程序,它可以让您在墙上发帖: https://apps.facebook.com/aspdotnetsample/?fb_source=search&ref=br_tf

这将使您能够设想您需要什么。

您的应用获得一个 appId,您需要使用它来生成授权 token 。

限制:您要发帖的用户墙应该已经添加了第 2 步创建的应用程序,这是强制性的,没有解决方法。

当用户第一次访问该应用时,它会自动请求发布到墙上的权限-->用户需要接受它。

希望这能让您了解如何着手进行此操作。

然后您可以在用户的​​墙上发帖。

对于 Banshee 使用 .NET 的 Facebook SDK 的以下请求:

userId - facebook 用户 id,发布消息的墙

此用户应该已添加您在第 1 步中创建的应用程序,否则它会显示未经授权的访问。

dynamic messagePost = new ExpandoObject(); 
messagePost.picture = "http://www.stackoverflow.com/test.png"; 
messagePost.link = "http://www.stackoverflow.com"; 
messagePost.name = "This is a test name"; 
 
messagePost.caption = "CAPTION 1"; 
messagePost.description = "Test desc"; 
messagePost.message = "message" 
 
var fb = new FacebookClient(); 
dynamic result = fb.Get("oauth/access_token", new {  
client_id     = "app_id",  
client_secret = "app_secret",  
grant_type    = "client_credentials"  
}); 
fb.AccessToken = result.access_token;     
 
try 
{ 
 var postId = fb.Post(userId + "/feed", messagePost); 
} 
catch (FacebookOAuthException ex) 
{ 
  //handle oauth exception 
} 
catch (FacebookApiException ex) 
{ 
  //handle facebook exception 
} 

您需要一个扩展 token 才能发布到墙上。Banshee 很好地解释了获取扩展 token 的步骤。

编辑:如何通过 Banshee 获取扩展 token : 请关注这个post