c# - 如何从我的 asp.net mvc 3 网站在 facebook 中发帖

标签 c# asp.net facebook asp.net-mvc-3

我假装将我的网站与 Facebook 集成,希望在用户与 Web 应用程序交互时自动发帖(在特定的 Facebook 帐户上)。

有什么方法可以使此操作像网络服务方式一样吗?验证并调用将我直接发送的信息发布到 facebook 墙上的 url?

我正在使用 asp.net mvc3 C#,我找到了一个 facebook 开发人员工具包库,这是正确的开始方式吗?我应该怎么做?

只需要在 facebook 帐户上自动写一篇文章,例如当我在我的网站上写一篇新文章(新闻)时,它会自动发布到 fb 上。

有什么让我开始的想法吗?

最佳答案

我做了类似的事情,当用户点击我的 mvc 应用程序上的“分享”按钮时,它会在他的墙上发布一些内容。使用 oauth 对话框的问题在于,它将浏览器重定向到 facebook 站点,供用户登录并接受应用程序权限。

在“分享”按钮上,我将它链接到这个 url:

                        <a href=""https://www.facebook.com/dialog/oauth?client_id=[YOUR_APP_ID]&redirect_uri=[THE_REDIRECT_PAGE]/&scope=publish_stream"">
                        <img src='@Url.Content("~/Images/facebook_share.png")' alt="Share on Facebook!" style="height:28px" />
                    </a>

YOUR_APP_ID 是您的 Facebook 应用程序 ID。 THE_REDIRECT_PAGE 是您网站上的公共(public)页面,一旦用户登录并接受权限,Facebook 将自动重定向该页面。当 facebook 重定向时,它会向其附加一个名为“代码”的查询字符串参数。 注意:重定向页面必须以“/”结尾,不能以文档结尾,否则无法正常工作!

一旦用户接受了您的请求,您必须向 facebook 询问另一个代码,称为访问代码,用于在用户的墙上发帖。

此代码位于重定向页面上:

        public ActionResult Index(string code)
    {
        string fbAuthCode = Request["code"]; //The authorization code.
        string fbAppId = "XXXXXXX"; //Your fb application id.
        string fbSecretAppId = "XXXXXXXXXXXXXXXXXXXXX"; //Your fb secret app id, it is found on the fb application configuration page.
        string redirectUrl = string.Format("[THE_REDIRECT_PAGE]", locationPointId, entryLocationId); //The redirect url. THIS MUST BE THE EXACT SAME REDIRECT URL USED ON THE JAVASCRIPT LINK!
        string fbUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + fbAppId + "&redirect_uri=" + redirectUrl + "&client_secret=" + fbSecretAppId + "&code=" + fbAuthCode; //Url used to post.
        string accessToken = string.Empty;

        try
        {
            WebClient client = new WebClient();
            using (Stream stream = client.OpenRead(fbUrl))
            using (StreamReader reader = new StreamReader(stream))
            {
                accessToken = reader.ReadToEnd().Split('&')[0].Replace("access_token=", string.Empty);
                reader.Close();
            }
        }
        catch (Exception ex)
        {
            throw new Exception("An error ocurred while trying to get the fb token in " + fbUrl, ex);
        }

获得访问 token 后,您可以发布到用户墙:

            string postUrl = "https://graph.facebook.com/me/feed";
        string postParameters;

        postParameters = string.Format("message={0}&picture={1}&name={2}&caption={2}&description={3}&link={4}&access_token={5}",
                                            "[Message]",
                                            "[PictureUrl]",
                                            "[Name]",
                                            "[Caption]",
                                            "[Link]",
                                            accessToken);

        try
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(postUrl);

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postParameters);
            req.ContentLength = bytes.Length;
            using (System.IO.Stream os = req.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length); //Push it out there
                os.Close();
                using (WebResponse resp = req.GetResponse())
                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                {
                    ViewBag.PostResult = sr.ReadToEnd().Trim();
                    sr.Close();
                }
                os.Close();
            }
        }
        catch (Exception ex)
        {
            throw new Exception("An error ocurred while posting data to the user's wall: " + postUrl + "?" + postParameters, ex);
        }

        return RedirectToAction(XXXXXXXXXXXxx....); //Then i redirect to another page.

您可以看到在异常情况下我抛出了发布的 url(用于调试目的)。 使用该 url,您通常可以转到 facebook Graph API Explorer 或 Linter 并检查真正的错误。

我不知道这是否正是您想要的,但希望它能给您一个开始... 我为此苦苦挣扎了几天,因为关于开放图谱的 facebook 文档还不是很好,至少对于我们不使用 curl 的人来说是这样 :)

https://developers.facebook.com/docs/opengraph/tutorial/ https://developers.facebook.com/docs/opengraph/

希望对您有所帮助。 公吨。

关于c# - 如何从我的 asp.net mvc 3 网站在 facebook 中发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9313506/

相关文章:

c# - 如何将 css 代码引用到 HtmlToImageConverter

c# - WebApi Controller 没有找到 Controller 的操作

c# - Alt 标签在 IE 中显示不正确

c# - 异步 ASP.Net MVC Controller 操作中的 Task.Run()

c# - 何时使用 System.Threading.ThreadPool 以及何时使用众多自定义线程池之一?

c# - 如何获取 Canvas 中 UserControl 的 X、Y 位置?

c# - 如何阻止枚举越界?

javascript - Facebook 登录 OAuth 后 iOS Chrome 共享

ios - 通过 Facebook IOS SDK 向您的 friend 发送私信

facebook - 第一次获取 Facebook 访问 token 的到期时间是多少