简体   繁体   English

facebook Graph API 使用 asp.net 和 c# 发送帖子不起作用

[英]facebook Graph API Send post using asp.net and c# is not working

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?我想通过 asp.net 和 c# 将帖子发送到我的 Facebook 帐户,我尝试了多种方法,但无法理解为什么它给我错误,有人可以指导我吗?

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我的 API 详情

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. (OAuthException - #2500) 必须使用活动访问令牌来查询有关当前用户的信息。

2 - var result = facebookClient.Post("/rashid.khi.31/feed", messagePost);

Error错误

(OAuthException - #803) (#803) Cannot query users by their username (rashid.khi.31) (OAuthException - #803) (#803) 无法通过用户名查询用户 (rashid.khi.31)

As my RND on google then i got fb User ID from fb Username on https://findmyfbid.in/作为我在 google 上的 RND,我从https://findmyfbid.in/ 上的fb 用户名中获得了 fb 用户 ID

3 - var result = facebookClient.Post("/100055422049992/feed", messagePost); 

Error错误

(OAuthException - #100) (#100) The global id 100055422049992 is not allowed for this call (OAuthException - #100) (#100) 全局 ID 100055422049992 不允许用于此调用

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 ?请帮助我我的代码有什么问题或其他什么问题,我知道,我发送了错误的 user_id 但我不知道从哪里可以获得正确的 ID?

just a quick note - the code you came up with is VERY old.只是一个简短的说明-您想出的代码很旧。 offline_access permission was removed years ago.多年前删除了offline_access权限。 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/topic-10/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):我找到了这个链接: https : //ermir.net/topic-10/how-to-publish-a-message-to-a-facebook-page-using-a-dotnet-console-application - 它显示了如何获取来自 FB 调试页面的永久令牌,然后如何使用它发布到页面(看起来类似于您的代码):

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.所以首先尝试从 FB 调试页面对令牌进行硬编码,看看是否可以发布。 And 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.然后努力从您的应用程序内部获取短期令牌 - 但这需要用户能够与 Facebook 通信 - 以允许您的应用程序与他们的页面进行通信。 Make sense?有道理?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM