【问题标题】:Send post to Facebook page将帖子发送到 Facebook 页面
【发布时间】:2020-03-21 14:21:27
【问题描述】:

我有一个 c# asp.net web 应用程序。

我正在通过他们的 javascript sdk 使用 facebook 登录,它会进行登录。

现在我有一个简单的所见即所得编辑器,其中添加的任何内容都应该发布在该登录用户的 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) 您请求的某些别名不存在:[user id]"}

我什至写了我的 facebook emailid 而不是 [user id] 但它仍然不起作用并给我错误:

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

谁能告诉我如何消除这个错误。

【问题讨论】:

  • 永远不要暴露您的应用程序秘密...尽快使其无效/刷新。它被称为秘密是有原因的。此外,您似乎有 2 个不同的应用程序机密。确保你完全理解发生了什么,不要只是复制/粘贴(非常旧的)代码并假设它会以某种方式工作。

标签: c# facebook-graph-api oauth


【解决方案1】:
  • 代码已经很老了,offline_accesspublish_stream 已经多年不存在了。
  • 您必须使用具有publish_pages 权限的页面令牌才能通过 API 发布到您的页面。
  • 您可以使用“me/feed”或“page-id/feed”,但不能使用电子邮件。

关于令牌以及如何生成令牌的信息:

在编程之前在 API Explorer 中尝试:https://developers.facebook.com/tools/explorer/

【讨论】:

  • 感谢 luschn 的回复,非常感谢。我的应用程序处于开发模式。我是否需要提交验证文件才能通过 API 在我的 Facebook 页面上发布消息?
  • 不用于测试,但只有在您不使用该应用“上线”时,新帖子才会显示给您(而不是普通用户)。
  • luschn - 当我点击“添加权限和功能”并请求“publish_pages”时,它显示“继续”按钮,要求我进行描述和验证......那么我应该在这里做什么?跨度>
  • 你为什么要这样做?您是否已经对您的项目进行了编程和测试?我说“不是为了测试”,如果你甚至没有编程任何东西,你就不需要经历审查过程。那么 facebook 能够审查什么?
  • “直播”意味着付费?
猜你喜欢
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多