Get access token to facebook page - WPF

∥☆過路亽.° 提交于 2019-11-30 16:24:28

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

  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.

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 WebBrowser control on it.
  5. Add code to navigate to the facebook login URL:

    string returnUrl = WebUtility.UrlEncode("https://www.facebook.com/connect/login_success.html");
    this.WebBrowser.Navigate(new Uri($"https://www.facebook.com/dialog/oauth?client_id={appId}&redirect_uri={returnUrl}&response_type=token%2Cgranted_scopes&scope=manage_pages&display=popup"));
    
  6. Add code to listen for the navigation to the success URL:

    this.WebBrowser.Navigated += (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();
        }
    };
    
  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
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!