I want to send post through asp.net and c# to my facebook account,i tried multiple things but unable to understand why it is giving me error, can any one guide me?
My App Details
private const string FacebookApiId = "XXXXXX";
private const string FacebookApiSecret = "XXXXXX";
private const 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
My Controller
public ActionResult Index()
{
string accessToken = GetAccessToken(FacebookApiId, FacebookApiSecret);
PostMessage(accessToken, "My message");
return View();
}
My Api Details
static void PostMessage(string accessToken, string message)
{
try
{
FacebookClient facebookClient = new FacebookClient(accessToken);
dynamic messagePost = new ExpandoObject();
messagePost.access_token = accessToken;
messagePost.message = message;
var result = facebookClient.Post("/798252384337611/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
string error = ex.Message.ToString();
}
catch (Exception ex)
{
string error1 = ex.Message.ToString();
}
}
static string GetAccessToken(string apiId, string apiSecret)
{
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();
Fbresponse rs = Newtonsoft.Json.JsonConvert.DeserializeObject<Fbresponse>(responseString);
accessToken = rs.access_token;
}
if (accessToken.Trim().Length == 0)
throw new Exception("There is no Access Token");
return accessToken;
}
My Tries
1 - var result = facebookClient.Post("/me/feed", messagePost);
Error
(OAuthException - #2500) An active access token must be used to query information about the current user.
2 - var result = facebookClient.Post("/rashid.khi.31/feed", messagePost);
Error
(OAuthException - #803) (#803) Cannot query users by their username (rashid.khi.31)
As my RND on google then i got fb User ID from fb Username on https://findmyfbid.in/
3 - var result = facebookClient.Post("/100055422049992/feed", messagePost);
Error
(OAuthException - #100) (#100) The global id 100055422049992 is not allowed for this call
Please help me what wrong with my code or some thing else, I know, I am sending wrong user_id but i dont't know from where i can get correct id ?
Just a quick note - the code you came up with is VERY old. offline_access
permission was removed years ago. So I would suggest you do more googling on how to create tokens. Perhaps your token is simply invalid.
I found this link: https://ermir.net/article/how-to-publish-a-message-to-a-facebook-page-using-a-dotnet-console-application - it shows how to get permanent token from FB Debug page, and then how to use it to post to the page (which looks similar to your code):
public class FacebookApi
{
private readonly string FB_PAGE_ID;
private readonly string FB_ACCESS_TOKEN;
private const string FB_BASE_ADDRESS = "https://graph.facebook.com/";
public FacebookApi(string pageId, string accessToken)
{
FB_PAGE_ID = pageId;
FB_ACCESS_TOKEN = accessToken;
}
public async Task<string> PublishMessage(string message)
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(FB_BASE_ADDRESS);
var parametters = new Dictionary<string, string>
{
{ "access_token", FB_ACCESS_TOKEN },
{ "message", message }
};
var encodedContent = new FormUrlEncodedContent(parametters);
var result = await httpClient.PostAsync($"{FB_PAGE_ID}/feed", encodedContent);
var msg = result.EnsureSuccessStatusCode();
return await msg.Content.ReadAsStringAsync();
}
}
}
So try to hardcode the token from FB Debug page first and see if you can publish. Then work on obtaining short-lived token from inside your app - but that would require user to be able to communicate with Facebook - to allow YOUR app to communicate with THEIR page. Makes sense?