C# 在MVC Facebook页面上发布为页面
没有太多麻烦,我设法在我的个人Facebook订阅源上发布状态信息。但我试图实现的是在我的页面上发布消息C# 在MVC Facebook页面上发布为页面,c#,asp.net-mvc,facebook,facebook-graph-api,C#,Asp.net Mvc,Facebook,Facebook Graph Api,没有太多麻烦,我设法在我的个人Facebook订阅源上发布状态信息。但我试图实现的是在我的页面上发布消息 public class Facebook { private const string FacebookApiId = "xxxxxxxxxxxxxxx"; private const string FacebookApiSecret = "xxxxxxxxxxxxxxxx"; string scop
public class Facebook
{
private const string FacebookApiId = "xxxxxxxxxxxxxxx";
private const string FacebookApiSecret = "xxxxxxxxxxxxxxxx";
string scope = "publish_stream,manage_pages";
private const string AuthenticationUrlFormat = "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials&scope=publish_stream,manage_pages";
static string GetAccesToken(string apiId, string apiSecret)
{
string url = string.Format(AuthenticationUrlFormat, apiId, apiSecret);
string accesToken = string.Empty;
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
String responseString = reader.ReadToEnd();
NameValueCollection query = HttpUtility.ParseQueryString(responseString);
accesToken = query["access_token"];
}
if (accesToken.Trim().Length == 0)
{
throw new Exception("no Access Token");
}
return accesToken;
}
static void PostMessage(string accessToken, JobOfferModel message)
{
try
{
FacebookClient facebookClient = new FacebookClient(accessToken);
dynamic messagePost = new ExpandoObject();
messagePost.access_token = accessToken;
messagePost.name = message.JobTitle;
messagePost.message = message.Content;
messagePost.caption = "www.Site.com";
facebookClient.Post("me/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
Console.WriteLine(ex);
}
catch (Exception ex)
{
正如luschn提到的,您需要使用页面访问令牌作为页面发布。您可以通过对“me/accounts”端点进行API调用来实现这一点。这将返回您管理的页面列表,以及每个页面的访问令牌 使用页面的访问令牌,向“me/feed”发出下一个请求以作为该页面发布。您可以在此处获得有关使用页面访问令牌的更多详细信息:
您还应该更新登录范围,以使用新的“发布\u操作”权限,而不是“发布\u流” 你们一直从哪里复制代码?publish_stream已弃用多年…无论如何,您需要使用页面标记。这个问题在stackoverflow上被问了很多次,只需搜索即可。谢谢你的回答。我对这有点陌生,所以我使用谷歌给我展示的最重要的结果:总是先看看facebook文档,它们提供示例代码和你需要的所有信息。