如何在我的页面粉丝墙上使用c#和asp.net在Facebook上发帖
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在我的页面粉丝墙上使用c#和asp.net在Facebook上发帖,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3928字,纯文字阅读大概需要6分钟。
内容图文
我试过3天后如何使用c#创建一个帖子我的粉丝页墙,我注意到两件事:
– Facebook没有提供更新的文档,没有完整和糟糕的例子(经常api更改)
– Facebook经常更改他的api,很多帖子都是obselete.
有人可以纠正我的代码或提供完整的优秀代码吗?
这是我的代码:
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身份验证进行网站身份验证”字段中设置您的localhost地址.
对我来说,它将是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个动作的autorisation: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#和asp.net在Facebook上发帖全部内容,希望文章能够帮你解决如何在我的页面粉丝墙上使用c#和asp.net在Facebook上发帖所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。