2

I am developing a WPF application that needs post on wall of a facebook's Page, and this without login window. Well, I want to get access token for my facebook page, and this is my code.

        var fb = new FacebookClient();
        string token = "";
        dynamic accounts = fb.Get("/"<USER_ID>"/accounts");
        foreach (dynamic account in accounts)
        {
            if (account.id == <PAGE_ID>)
            {
                token = account.access_token;
                break;
            }
        }

But I receive a error #104. It is a simple error, that I need a access token to do this operation. Then I use other code to get the user access token

        var fb = new FacebookClient();
        dynamic result = fb.Get("oauth/access_token", new
        {
            client_id = <PAGE_ID>,
            client_secret = <APP_SECRET>,
            grant_type = "fb_exchange_token",
            fb_exchange_token = <USER_TOKEN>
        });

But I get error #101:

"Error validating application. Cannot get application info due to a system error."

Someone knows what I have to do?

Thanks!!!

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
Marcos
  • 40
  • 1
  • 6
  • Can this help you http://stackoverflow.com/questions/11237610/how-to-get-a-facebook-access-token-for-a-page-with-no-app-or-app-secret ? – aybe Apr 10 '14 at 02:19
  • [1] Did you want to create this application for all the users or for yourself only? I'll provide you the answer accordingly taking security into consideration! [2] If you want to do it just for yourself you can have a never expiring page token which you can get using a separate web app/wpf/ even not using any app just using the [Graph API Explorer](http://developers.facebook.com/tools/explorer). So let me know how would you prefer getting this never expiring token that you can use directly in your application – Sahil Mittal Apr 10 '14 at 07:21
  • @Aybe thanks, but this don't work for me. – Marcos Apr 10 '14 at 12:43
  • @SahilMittal, I want the token just for me. thanks. – Marcos Apr 10 '14 at 12:43
  • You took a long break after posting the question lol. btw, so getting a token using graph api explorer is fine for you? and you can save that token some where and use it in the application. Or did you want to do this by creating an application? – Sahil Mittal Apr 10 '14 at 12:46
  • Sorry but its really tough to communicate. You must at least be active enough here to get your query solved – Sahil Mittal Apr 10 '14 at 12:54
  • Sorry @SahilMittal it is because I need to do a lot of things for my mother... – Marcos Apr 10 '14 at 22:53
  • Well, I am reading a xml file to get informations (token, configuration). Then I put the token that I get in Graph API Explorer by copy and paste method on this xml file. After I get the second error #101. But if I read the userId to use my first code, I get the first error. – Marcos Apr 10 '14 at 23:02
  • @SahilMittal Thanks I am able to get now! I go to Graph API Explorer and after I select specific app that I want the token. It is simple. This solved my problem. Thank you very much!! :D – Marcos Apr 11 '14 at 00:48
  • In resume I take this token generated by Graph API Explorer to specific app and generate a long lived token to my application. – Marcos Apr 11 '14 at 02:35
  • Oh, yes! This is the answer of my question. Thanks!!. – Marcos Apr 12 '14 at 14:18

2 Answers2

3

I'm not sure if you've been able to get a never expiring token for the page, so I'll explain you the steps:

  1. Open Graph API Explorer

  2. Select your app from the drop-down

    enter image description here

  3. Click "Get Access Token" button, and select the manage_pages permission.

  4. Copy the token and run this in the browser:

     https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={step-3-token}
    
  5. Copy the token from step-4 and paste in to the access_token field and call:

     /{page-id}?fields=access_token
    
  6. The token you get now is a never-expiring token, you can validate the same in Debugger .Use this in your app.

But beware, its not recommended to use this token on client side if your app is public.

Community
  • 1
  • 1
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • This token actually expires after 60 days. The response to the fb_exchange_token call has &expires=5184000 at the end (which is in seconds). – RandomEngy Sep 10 '16 at 20:10
0

If you use the fb_exchange_token call, it will give you a token that expires after 60 days. In order to make it work correctly, I had to go through the login web flow to guarantee I got an up-to-date page access token.

  1. Go to the Facebook App dashboard
  2. If you haven't already added the Facebook Login product, click "+ Add Product" and select Facebook Login
  3. Enable the "embedded browser control" option and enter https://www.facebook.com/connect/login_success.html as the allowed redirect URL.
  4. Make a Window with a WebView control on it. The WebBrowser control no longer works; the browser engine powering it is too old.
  5. Add code to listen for the navigation to the success URL:

    this.webView.NavigationCompleted += (sender, args) =>
    {
        if (args.Uri.AbsolutePath == "/connect/login_success.html")
        {
            if (args.Uri.Query.Contains("error"))
            {
                MessageBox.Show("Error logging in.");
            }
            else
            {
                string fragment = args.Uri.Fragment;
                var collection = HttpUtility.ParseQueryString(fragment.Substring(1));
                string token = collection["access_token"];
    
                // Save the token somewhere to give back to your code
            }
    
            this.Close();
        }
    };
    
  6. Add code to navigate to the facebook login URL:

    string returnUrl = WebUtility.UrlEncode("https://www.facebook.com/connect/login_success.html");
    this.webView.Source = new Uri($"https://www.facebook.com/dialog/oauth?client_id={appId}&redirect_uri={returnUrl}&response_type=token%2Cgranted_scopes&scope=manage_pages&display=popup");
    
  7. Call window.ShowDialog() to pop up the login window, then grab the user access token.

  8. Create some models to help you out:

    public class AccountsResult
    {
        public List<Account> data { get; set; }
    }
    
    public class Account
    {
        public string access_token { get; set; }
    
        public string id { get; set; }
    }
    
  9. Using the user access token, get the page access token:

    FacebookClient userFacebookClient = new FacebookClient(userAccessToken);
    var accountsResult = await userFacebookClient.GetTaskAsync<AccountsResult>("/me/accounts");
    string pageAccessToken = accountsResult.data.FirstOrDefault(account => account.id == PageId)?.access_token;
    
    if (pageAccessToken == null)
    {
        MessageBox.Show("Could not find page under user accounts.");
    }
    else
    {
        FacebookClient pageFacebookClient = new FacebookClient(pageAccessToken);
    
        // Use pageFacebookClient here
    }
    
RandomEngy
  • 14,931
  • 5
  • 70
  • 113