简体   繁体   English

将帖子发送到 Facebook 页面

[英]Send post to Facebook page

I have ac# asp.net web application.我有 ac# asp.net web 应用程序。

I am using the facebook login through their javascript sdk and it does the login.我正在通过他们的 javascript sdk 使用 facebook 登录,它会登录。

Now I have a Simple WYSIWYG Editor in which whatever is added should be posted on the facebook page of that logged in user.现在我有一个简单的 WYSIWYG 编辑器,其中添加的任何内容都应该发布在登录用户的 Facebook 页面上。

Following is the code:以下是代码:

   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
    }
}

This throws error:这会引发错误:

{"(OAuthException - #803) (#803) Some of the aliases you requested do not exist: [user id]"} {"(OAuthException - #803) (#803) 您请求的某些别名不存在:[用户 ID]"}

I even wrote my facebook emailid instead of [user id] but it still does not work and gives me error:我什至写了我的 facebook emailid 而不是 [user id] 但它仍然不起作用并给我错误:

Message: "(OAuthException - #2500) An active access token must be used to query information about the current user."消息:“(OAuthException - #2500)必须使用活动访问令牌来查询有关当前用户的信息。”

Can anyone please advise how can I remove this error.任何人都可以请建议我如何删除此错误。

  • The code is VERY old, offline_access and publish_stream do not exist anymore since many years.代码很旧, offline_accesspublish_stream已经不存在多年了。
  • You have to use a Page Token with the publish_pages permission in order to post to your Page with the API.您必须使用具有publish_pages权限的页面令牌才能使用 API 发布到您的页面。
  • You can use "me/feed" or "page-id/feed", but not an Email.您可以使用“me/feed”或“page-id/feed”,但不能使用电子邮件。

Information about Tokens and how to generate them:有关令牌以及如何生成它们的信息:

Try stuff in the API Explorer before programming: https://developers.facebook.com/tools/explorer/编程前在 API Explorer 中尝试一些东西: https : //developers.facebook.com/tools/explorer/

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

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