[英]Send post to Facebook page
我有 ac# asp.net web 应用程序。
我正在通过他们的 javascript sdk 使用 facebook 登录,它会登录。
现在我有一个简单的 WYSIWYG 编辑器,其中添加的任何内容都应该发布在登录用户的 Facebook 页面上。
以下是代码:
string FacebookApiId = "952214336368241";
string FacebookApiSecret = "fb2213d66523c8cb0bc99306f46ee457";
string accessToken = GetAccessToken(FacebookApiId, FacebookApiSecret);
PostMessage(accessToken, "My message");
private string GetAccessToken(string apiId, string apiSecret)
{
string AuthenticationUrlFormat = "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials&scope=manage_pages,offline_access,publish_stream";
string accessToken = string.Empty;
string url = string.Format(AuthenticationUrlFormat, apiId, apiSecret);
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(); // this gives me {"access_token":"952214336368241|McqSGYIMU1eYBZ1zGydWj9hu6Bt","token_type":"bearer"}
NameValueCollection query = HttpUtility.ParseQueryString(responseString);
accessToken = query["access_token"]; // This line throws error
}
//if (accessToken.Trim().Length == 0)
//throw new Exception("There is no Access Token");
return "952214336368241|McqSGYIMU1eYBZ1zGydWj9hu6B"; // hard-coded to get it working;
}
private void PostMessage(string accessToken, string v)
{
try
{
FacebookClient facebookClient = new FacebookClient(accessToken);
dynamic messagePost = new ExpandoObject();
messagePost.access_token = accessToken;
messagePost.message = v;
var result = facebookClient.Post("/[user id]/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
//handle something
}
catch (Exception ex)
{
//handle something else
}
}
这会引发错误:
{"(OAuthException - #803) (#803) 您请求的某些别名不存在:[用户 ID]"}
我什至写了我的 facebook emailid 而不是 [user id] 但它仍然不起作用并给我错误:
消息:“(OAuthException - #2500)必须使用活动访问令牌来查询有关当前用户的信息。”
任何人都可以请建议我如何删除此错误。
offline_access
和publish_stream
已经不存在多年了。publish_pages
权限的页面令牌才能使用 API 发布到您的页面。有关令牌以及如何生成它们的信息:
编程前在 API Explorer 中尝试一些东西: https : //developers.facebook.com/tools/explorer/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.