[英]How to do a post in facebook on my page fan wall with c# and asp.net
I tryed since 3 days how i create a post my fan page wall with c#, and i note 2 things : - Facebook provides not updated documentation with no complete and poor example (api changes often) - Facebook often changes his api and lots of post are obselete. 我尝试了3天后如何用c#创建一个帖子我的粉丝页面墙,我注意到两件事情: - Facebook提供没有更新的文档,没有完整和糟糕的例子(经常api更改) - Facebook经常更改他的api和很多帖子是痴迷的。 Is somebody can correct my code or provide me the complete good code ? 有人可以纠正我的代码或提供完整的优秀代码吗?
This is my code : 这是我的代码:
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);
}
}
I hope this post will be helpfull for lots of people, i try to be simple and clear : 我希望这篇文章能为很多人提供帮助,我试着简单明了:
1-Create your facebook developper account, and to test your code in your computern ( localhost
), set your localhost adress in the field "website authentication with facebook authentication". 1 - 创建您的facebook开发者帐户,并在您的计算机( localhost
)中测试您的代码,在“使用facebook身份验证进行网站身份验证”字段中设置您的localhost地址。 For me it will be http://localhost:2551/Default.aspx
for example because i test in the Defaut.aspx of my wweb application. 对我来说,它将是http://localhost:2551/Default.aspx
,例如因为我在我的wweb应用程序的Defaut.aspx中测试。 You 'll change this adress when you'll deploy on your website (for me i ll change with http://www.mywebsiteurl.com/Default.aspx just before deploy the code on my website). 当你在网站上部署时,你将改变这个地址(对我而言,在我的网站上部署代码之前,我将使用http://www.mywebsiteurl.com/Default.aspx进行更改)。
2- With your facebook user account, create your fan page. 2-使用您的Facebook用户帐户,创建您的粉丝页面。
3-When you created your fan page , go to your fan page see the URL to obtain your PAGE_ID For example mine is http://www.facebook.com/pages/toto/446533181408238?ref=ts&fref=ts so my PAGE_ID is 446533181408238 3 - 当您创建粉丝页面时,请转到您的粉丝页面,查看URL以获取您的PAGE_ID例如我的http://www.facebook.com/pages/toto/446533181408238?ref=ts&fref=ts所以我的PAGE_ID是446533181408238
3- It's almost finished, just a little explanation : because i created the fan page , im administrator of the fan page and i must ask the authoraization to facebook to post since my developper account so i must get the autorisation for 2 actions : publish_stream and manage_pages. 3-它几乎完成了,只是一个小小的解释:因为我创建了粉丝页面,我是粉丝页面的管理员,我必须要求自动发布到Facebook发布自我的开发者帐户,所以我必须得到2个动作的autorisation:publish_stream和manage_pages。
Let's go for coding : 我们去编码:
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);
}
}
There are some things wrong with the parameters you pass with your Graph API request. 您通过Graph API请求传递的参数存在一些问题。
req_perms
parameter is deprecated and has been replaced by scope
, 不推荐使用req_perms
参数并将其替换为scope
, action
field requires an array whereas the privacy
field requires a JSON . action
字段需要一个数组,而privacy
字段需要一个JSON 。 How an array looks like: "actions": {'value': 'CUSTOM', 'allow': '{friend-list-id}'}
. 数组的外观如下: "actions": {'value': 'CUSTOM', 'allow': '{friend-list-id}'}
。
How a JSON looks like: JSON的外观如何:
"privacy": [
{
"name": "Comment",
"link": "https://www.facebook.com/1234567890/posts/09876543210987654321"
},
{
"name": "Like",
"link": "https://www.facebook.com/1234567890/posts/09876543210987654321"
}
]
i am using the following code to post image to my Facebook page wall 我使用以下代码将图像发布到我的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.