Get access token to facebook page - WPF

前端 未结 2 992
南笙
南笙 2021-01-03 13:44

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 th

2条回答
  •  孤城傲影
    2021-01-03 14:17

    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 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("/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
      }
      

提交回复
热议问题