如何使用c#和asp.net在facebook上的我的页面粉丝墙上发布帖子
3天以来,我一直在尝试如何用c#创建我的粉丝页面墙,我注意到两件事: -Facebook提供未更新的文档,没有完整且糟糕的示例(api经常更改) -Facebook经常改变他的api,很多帖子都被屏蔽了。 是否有人可以更正我的代码或提供完整的好代码 这是我的代码:如何使用c#和asp.net在facebook上的我的页面粉丝墙上发布帖子,c#,asp.net,facebook,facebook-graph-api,C#,Asp.net,Facebook,Facebook Graph Api,3天以来,我一直在尝试如何用c#创建我的粉丝页面墙,我注意到两件事: -Facebook提供未更新的文档,没有完整且糟糕的示例(api经常更改) -Facebook经常改变他的api,很多帖子都被屏蔽了。 是否有人可以更正我的代码或提供完整的好代码 这是我的代码: if (String.IsNullOrEmpty(Request.QueryString["code"])) { Response.Redirect("https://gra
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);
}
}
在Graph API请求中传递的参数存在一些问题
参数已被弃用,并已被req\u perms
替换范围
- 权限通过登录链接传递,而不是通过Graph API请求传递李>
操作
字段需要一个数组,而隐私
字段需要一个JSON“操作”:{'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"
}
]
我希望这篇文章能对很多人有所帮助,我尽量做到简单明了: 1-创建您的facebook Developer帐户,并在计算机中测试您的代码n(
localhost
),在“网站身份验证与facebook身份验证”字段中设置您的localhost地址。
对我来说,这将是http://localhost:2551/Default.aspx
例如,因为我在wweb应用程序的Defaut.aspx中进行测试。
您将在网站上部署时更改此地址(对于我来说,在将代码部署到我的网站之前,我将更改为)
2-使用你的facebook用户帐户,创建你的粉丝页面
3-当您创建您的粉丝页面时,请转到您的粉丝页面,查看URL以获取您的页面ID
比如我的
所以我的页码是446533181408238
3-它几乎完成了,只是一个小小的解释:因为我创建了粉丝页面,我是粉丝页面的管理员,我必须要求facebook的授权发布我的开发者帐户,所以我必须获得两个操作的自动授权:发布流和管理页面
让我们开始编码:
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);
}
}
private void Do()
{
字符串app_id=“157873644371675”;
字符串app_secret=“c27a10c347af4280720fa3d76c9ae08c”;
string scope=“发布\u流,管理\u页面”;
如果(请求[“代码”]==null)
{
重定向(string.Format(
"https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}“,
app_id,Request.Url.AbsoluteUri,scope));
}
其他的
{
字典标记=新字典();
字符串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,作用域,Request[“code”]。ToString(),app_secret);
HttpWebRequest request=System.Net.WebRequest.Create(url)作为HttpWebRequest;
使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse)
{
StreamReader=新的StreamReader(response.GetResponseStream());
字符串vals=reader.ReadToEnd();
foreach(vals.Split('&')中的字符串标记)
{
//meh.aspx?token1=steve&token2=jake&。。。
tokens.Add(token.Substring(0,token.IndexOf(“=”)),
子字符串(token.IndexOf(“=”)+1,token.Length-token.IndexOf(“=”-1));
}
}
字符串访问令牌=令牌[“访问令牌”];
var client=新的FacebookClient(访问令牌);
动态参数=新的ExpandooObject();
parameters.message=“查看这篇有趣的文章”;
parameters.link=”http://www.natiska.com/article.html";
参数。图片=”http://www.natiska.com/dav.png";
parameters.name=“文章标题”;
parameters.caption=“链接的标题”;
//446533181408238是我的粉丝页面
client.Post(“/446533181408238/feed”,参数);
}
}
我正在使用以下代码将图像发布到我的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);
}
你得到了什么错误(OAutheException-#200)(#200)用户未授权应用程序执行此操作它不作为管理员发布,并以我的名字作为海报显示在“他人在我的页面名称上的最新帖子”中!!