简体   繁体   中英

Send post to Facebook page

I have ac# asp.net web application.

I am using the facebook login through their javascript sdk and it does the login.

Now I have a Simple WYSIWYG Editor in which whatever is added should be posted on the facebook page of that logged in user.

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

I even wrote my facebook emailid instead of [user id] but it still does not work and gives me error:

Message: "(OAuthException - #2500) An active access token must be used to query information about the current user."

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.
  • You have to use a Page Token with the publish_pages permission in order to post to your Page with the API.
  • You can use "me/feed" or "page-id/feed", but not an Email.

Information about Tokens and how to generate them:

Try stuff in the API Explorer before programming: https://developers.facebook.com/tools/explorer/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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