0

The reported error occurs while i am trying to login into the facebook(web browser in winforms) to get the user access tokenI used the below code to post the text box content into my facebook wall and it's works fine.

private void btn_Post_Click(object sender, EventArgs e)
        {
            string appID, appSecret, userId;
            appID = ConfigurationManager.AppSettings["AppID"].ToString();
            appSecret=ConfigurationManager.AppSettings["AppSecret"].ToString();
            userId = ConfigurationManager.AppSettings["UserID"].ToString();
            var fb = new FacebookClient();
            dynamic result = fb.Get("oauth/access_token", new
            {
                client_id = appID,
                client_secret = appSecret,                    
                grant_type = "client_credentials"
            });
            fb.AccessToken = result.access_token;
            PostToWall(txt_status.Text, userId, fb.AccessToken);
        }

        private static void PostToWall(string message, string userId, string wallAccessToken)
        {
            try
            {
                var fb = new FacebookClient(wallAccessToken);
                string url = string.Format("{0}/{1}", userId, "feed");
                var argList = new Dictionary<string, object>();
                argList["message"] = message;
                fb.Post(url, argList);
                MessageBox.Show("Posted");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message);
            }
        }

Then i have created the facebook page and trying to post on it. For that i enabled the manage page permission too but i could not able to get the page access token. How can i able to get the facebook page access token dynamically through the C# code to post on it.

Karthik
  • 67
  • 1
  • 9

1 Answers1

0

There are two API endpoints to get a Page Token:

Get a List of all Pages with Page Tokens

/me/accounts?fields=name,access_token

Get a Page Token for a specific Page:

/page-id?fields=access_token

Make sure you authorize with manage_pages AND publish_pages if you want to use the Token for posting as Page.

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • The reported error occurs while i am trying to login into the facebook(web browser in windows form) to get the user access token. How to fix that one. – Karthik Jul 27 '16 at 12:26
  • that changes the whole question...you need to implement a proper login and make sure the user is actually authorized. – andyrandy Jul 27 '16 at 12:28
  • While click the post on facebook button i have tried to display the facebook login page to enter the credential but it's report that error.. string OAuthURL = @"https://www.facebook.com/dialog/oauth?client_id=[app_Id] &redirect_uri=https://www.facebook.com/connect/login_success.html &response_type=token &scope=user_groups,user_status,read_friendlists,manage_friendlists,user_photos&type=user_agent&display=popup"; web_facebook.Navigate(OAuthURL); – Karthik Jul 27 '16 at 12:42
  • well...and why not use the permissions i mentioned in my answer? anyway, make sure you get a token that works and use that token. only you can debug your stuff. – andyrandy Jul 27 '16 at 12:47
  • here you can debug your token after authorization: https://developers.facebook.com/tools/debug/accesstoken/ – andyrandy Jul 27 '16 at 12:47
  • I have provided the mentioned permission and in debug also it is does not report any issues but it's report the error like "HTTP 400 Bad Request" while loading the facebook authentication page in the web browser control. – Karthik Jul 28 '16 at 07:03
  • Still, the issue occurs which i have posted as a screen shot.Please some one help me on this. – Karthik Aug 01 '16 at 05:28