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

问题描述 投票:0回答:1

我想通过 asp.net 和 c# 将帖子发送到我的 Facebook 帐户,我尝试了多种方法,但无法理解为什么它给我错误,任何人都可以指导我吗?

我的应用程序详细信息

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

我的控制器

public ActionResult Index()
{
     string accessToken = GetAccessToken(FacebookApiId, FacebookApiSecret);
     PostMessage(accessToken, "My message");
     return View();
}

我的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;
        }

我的尝试

 1 -   var result = facebookClient.Post("/me/feed", messagePost);

错误

(OAuthException - #2500)必须使用活动访问令牌来查询有关当前用户的信息。

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

错误

(OAuthException - #803)(#803)无法通过用户名查询用户(rashid.khi.31)

作为我在 google 上的 RND,然后我从 https://findmyfbid.in/

上的 fb 用户名获得了 fb 用户 ID
3 - var result = facebookClient.Post("/100055422049992/feed", messagePost); 

错误

(OAuthException - #100) (#100) 此调用不允许全局 ID 100055422049992

请帮助我,我的代码或其他问题有什么问题,我知道,我发送了错误的 user_id,但我不知道从哪里可以获得正确的 id?

c# facebook asp.net-mvc-4 post facebook-graph-api
1个回答
3
投票

请注意 - 您想出的代码非常旧。

offline_access
许可几年前已被删除。因此,我建议您更多地搜索如何创建令牌。也许您的令牌根本无效。

我找到了这个链接:https://ermir.net/article/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();
        }

    }
}

因此,请先尝试从 FB 调试页面对令牌进行硬编码,看看是否可以发布。然后努力从您的应用程序内部获取短期令牌 - 但这需要用户能够与 Facebook 通信 - 以允许您的应用程序与他们的页面通信。有道理吗?

© www.soinside.com 2019 - 2024. All rights reserved.