簡體   English   中英

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

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

我想通過 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?

只是一個簡短的說明-您想出的代碼很舊。 多年前刪除了offline_access權限。 所以我建議你在如何創建令牌上做更多的谷歌搜索。 也許您的令牌根本無效。 我找到了這個鏈接: 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();
        }

    }
}

所以首先嘗試從 FB 調試頁面對令牌進行硬編碼,看看是否可以發布。 然后努力從您的應用程序內部獲取短期令牌 - 但這需要用戶能夠與 Facebook 通信 - 以允許您的應用程序與他們的頁面進行通信。 有道理?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM