c# - 如何使用 c# 和 asp.net 在我的页面粉丝墙上的 facebook 中发帖

标签 c# asp.net facebook facebook-graph-api

自 3 天以来,我一直在尝试如何使用 C# 创建我的粉丝页面墙,我注意到两件事: - Facebook 提供未更新的文档,没有完整和糟糕的示例(api 经常更改) - Facebook 经常改变他的 api,很多帖子都过时了。 有人可以更正我的代码或向我提供完整的好代码吗?

这是我的代码:

  if (String.IsNullOrEmpty(Request.QueryString["code"]))
            {
                Response.Redirect("https://graph.facebook.com/oauth/authorize?client_id=157873644371673&redirect_uri=http://localhost:2551/Default.aspx&scope=publish_stream,manage_pages,offline_access&display=popup");
            }
            else
            {

    FacebookClient fb = new FacebookClient();
                    dynamic result1 = fb.Get("oauth/access_token", new
                    {
                        client_id = "MY_APP_ID",
                        client_secret = "MY_SECRET_ID",
                        grant_type = "client_credentials",
                        redirect_uri = "www.mysite.com"
                    });

                    fb.AppId = "MY_APP_ID";
                    fb.AppSecret = "MY_SECRET_ID";
                    fb.AccessToken = result1.access_token;

                     dynamic parameters = new ExpandoObject();
                    parameters.message = "Check out this funny article";
                    parameters.link = "http://www.example.com/article.html";
                    parameters.picture = "http://www.example.com/article-thumbnail.jpg";
                    parameters.name = "Article Title";
                    parameters.caption = "Caption for the link";
                    parameters.description = "Longer description of the link";
                    parameters.req_perms = "manages_pages";
                    parameters.scope = "manages_pages";

                    parameters.actions = new
                    {
                        name = "View on Zombo",
                        link = "www.zombo.com",
                    };
                    parameters.privacy = new
                    {
                        value = "ALL_FRIENDS",
                    };

                    try
                    {
                        var result = fb.Post("/" + "MY_FACEBOOK_FAN_PAGE_ID" + "/feed", parameters);
                    }
                    catch (FacebookOAuthException ex)
                    {
                        //handle something
                        Response.Write(ex.Message);
                    }
}

最佳答案

我希望这篇文章对很多人有帮助,我尽量简单明了:

1-创建您的 facebook 开发者帐户,并在您的计算机 (localhost) 中测试您的代码,在“使用 facebook 身份验证的网站身份验证”字段中设置您的本地主机地址。 对我来说,它将是 http://localhost:2551/Default.aspx 例如,因为我在我的 wweb 应用程序的 Defaut.aspx 中进行测试。 当您在您的网站上部署时,您将更改此地址(对我来说,我将在将代码部署到我的网站之前更改为 http://www.mywebsiteurl.com/Default.aspx)。

2- 使用您的 Facebook 用户帐户,创建您的粉丝页面。

3-当你创建了你的粉丝专页后,去你的粉丝专页查看URL获取你的PAGE_ID 例如我的是http://www.facebook.com/pages/toto/446533181408238?ref=ts&fref=ts 所以我的 PAGE_ID 是 446533181408238

3- 快完成了,只是一点点解释:因为我创建了粉丝页面,我是粉丝页面的管理员,我必须请求 facebook 的授权才能发帖,因为我的开发者帐户所以我必须获得 2 个操作的授权: publish_stream 和 manage_pages。

让我们开始编码:

  private void Do()
        {
            string app_id = "157873644371675";
            string app_secret = "c27a10c347af4280720fa3d76c9ae08c";
            string scope = "publish_stream,manage_pages";

            if (Request["code"] == null)
            {
                Response.Redirect(string.Format(
                    "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                    app_id, Request.Url.AbsoluteUri, scope));
            }
            else
            {
                Dictionary<string, string> tokens = new Dictionary<string, string>();

                string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
                    app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

                HttpWebRequest request = System.Net.WebRequest.Create(url) as HttpWebRequest;

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    string vals = reader.ReadToEnd();

                    foreach (string token in vals.Split('&'))
                    {
                        //meh.aspx?token1=steve&token2=jake&...
                        tokens.Add(token.Substring(0, token.IndexOf("=")),
                            token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                    }
                }

                string access_token = tokens["access_token"];

                var client = new FacebookClient(access_token);

                dynamic parameters = new ExpandoObject();
                parameters.message = "Check out this funny article";
                parameters.link = "http://www.natiska.com/article.html";
                parameters.picture = "http://www.natiska.com/dav.png";
                parameters.name = "Article Title";
                parameters.caption = "Caption for the link";

                //446533181408238 is my fan page
                client.Post("/446533181408238/feed", parameters);

            }
              }

关于c# - 如何使用 c# 和 asp.net 在我的页面粉丝墙上的 facebook 中发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487184/

相关文章:

Android App新 keystore 给出错误配置错误

ios - 如何在 Xcode 8、swift 3 中将 Facebook 值发送到下一个屏幕

c# - vs2010循环依赖问题

asp.net - currentUser 作为模型绑定(bind)参数

c# - 如何在查询 XML 时在 LINQ 中添加 OR 条件?

javascript - 选择单选按钮时启用单击按钮

asp.net - Bin 部署的 DLL 优先于 GAC 的 DLL 用于 Web 应用程序?

php - Facebook - 如何从 ID 获取电子邮件?

c# - 如何在表单帖子中获取查询字符串值

c# - 如何从 Windows 服务打印 PDF 文档