开发者

Avoiding the facebook login webform on startup

I am pretty much using the samples in the SDK for my app. And it works great. Except I have two questions, relating to authentication. 1. How can I avoid the webform from popping up each time I start up the app? Once I have authorized my app, each time I restart it, I see the webform appear for a second and then it closes. Not very clean, in my opinion. Is there a way to check if I am still logged in, and have given my app authorization before displaying the webform? 2. If I become unauthenticated while my app is running, is there a way for me to easily detect that and then show the login form again?

I think I am close with my code below, but not quite there.

Thanks in advance!

private string[] extendedPermissions = new[] { "read_stream", "offline_access" };

public Form1()
{
    var fbLoginDialog = new FacebookLoginDialog(AppId, extendedPermissions);

    //always shows login dialog, even if the app was just recently closed. Is this necessary?
    fbLoginDialog.ShowDialog(); 

    //every minute look for new posts
    clockTimer.Elapsed += new System.Timers.ElapsedEventHandler(clockTimer_Elapsed); 

    DisplayAppropriateMessage(fbLoginDialog.FacebookOAuthResult);
    GetAllData(unixTimeOfNewestPost);
    InitializeComponent();
}

private void DisplayAppropriateMessage(FacebookOAuthResult facebookOAuthResult)
{
  if (facebookOAuthR开发者_StackOverflow社区esult != null)
  {
    if (facebookOAuthResult.IsSuccess)
    {
      _accessToken = facebookOAuthResult.AccessToken;
    }
    else
    {
      MessageBox.Show(facebookOAuthResult.ErrorDescription);
    }
  }
}

private void GetAllData(double unixTs)
{
  var fb = new FacebookClient(_accessToken);
  Facebook.JsonArray posts = new JsonArray();
  try
  {
      var result = (IDictionary<string, object>)fb.Get("/me/home?since=" + unixTs + "&limit=1000");
      posts = (Facebook.JsonArray)result["data"];
  }
  catch (FacebookOAuthException e)
  {
    MessageBox.Show("FacebookOAuthException", 
      "Facebook Authorization problem. The error message was '" + e.Message + "'.", null, "");
    //show the login form again???
  }
  catch (Exception e)
  {
      Log("Exception. There was a general exception. The error message was '" + e.Message + "'.");
  }

  foreach (Facebook.JsonObject post in posts)
  {
    // do stuff with each post
  }
} // end of GetPosts()


1.How can I avoid the webform from popping up each time I start up the app? Once I have authorized my app, each time I restart it, I see the webform appear for a second and then it closes. Not very clean, in my opinion. Is there a way to check if I am still logged in, and have given my app authorization before displaying the webform?

You will need to save the access token safely somewhere else. Incase you have that access token you don't need to even ask for the login dialog.

2.If I become unauthenticated while my app is running, is there a way for me to easily detect that and then show the login form again?

You will need to catch FacebookOAuthException to every requests you make and then ask the user to login

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜