4

从 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);
                    }
}
4

3 回答 3

7

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

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

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

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

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

让我们开始编码:

  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);

            }
              }
于 2013-03-19T08:29:15.943 回答
0

您通过 Graph API 请求传递的参数有一些问题。

  1. 您不会有效地请求权限:
    • req_perms参数已弃用并已替换为scope,
    • 权限是通过登录链接传递的,而不是通过 Graph API 请求传递的!
  2. action字段需要一个数组,而该privacy字段需要一个JSON

数组的样子:"actions": {'value': 'CUSTOM', 'allow': '{friend-list-id}'}.

JSON 的样子:

"privacy": [   
    {
      "name": "Comment", 
      "link": "https://www.facebook.com/1234567890/posts/09876543210987654321"
    }, 
    {
      "name": "Like", 
      "link": "https://www.facebook.com/1234567890/posts/09876543210987654321"
    }
]
于 2013-03-18T22:07:30.297 回答
0

我正在使用以下代码将图像发布到我的 Facebook 页面墙上

 string app_id = "*************";
string app_secret = "*************";
string scope = "publish_actions,manage_pages";
string accessToken = GetAccessToken(FacebookAppId, FacebookAppSecret);
            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
            {
                FacebookClient fb = new FacebookClient();
                dynamic result1 = fb.Get("oauth/access_token", new
                {
                    client_id = "******",
                    client_secret = "*************",
                    grant_type = "publish_actions,manage_pages",
                    redirect_uri = "*************"
                });

                fb.AppId = "MY_APP_ID";
                fb.AppSecret = "MY_SECRET_ID";
                fb.AccessToken = result1.access_token;
// to make new object
                dynamic parameters = new ExpandoObject();
                parameters.message = "Check out this funny article";
                parameters.link = "*************";
                parameters.picture = "*************";
                parameters.name = "*************";
                parameters.caption = "*************";
                parameters.description = "*************";
                parameters.req_perms = "*************";
                parameters.scope = "*************";

                parameters.actions = new
                {
                    name = "*************",
                    link = "*************",
                };
                parameters.privacy = new
                {
                    value = "*************",
                };

                try
                {
                    var result = fb.Post("/" + "*************" + "/feed", parameters);
                }
于 2016-11-22T07:28:18.807 回答