FacebookOAuthException未得到处理

时间:2011-01-16 16:41:26

标签: c# wpf facebook windows-phone-7 facebook-c#-sdk

我是这些东西的新手,我一直在测试api ...... 并找到了一个情况:

如果用户更改了Facebook的密码

更新了Access令牌...并尝试发布API启动 一个例外,应用程序崩溃了...... 如何解决这种情况?

         try {
              FacebookApp app = new FacebookApp(FacebookToken);
              var args = new Dictionary<string, object>();
              args["message"] = "hi";
              args["caption"] = "appcaption";
              args["description"] = "appdescription";
              args["name"] = "appname";
              args["picture"] = "apppicture";
              args["link"] = "applink";
              app.ApiAsync((X) => { calback(X); }, null, "/me/feed", args, HttpMethod.Post);
          }
           catch (Exception ex) {
               Uri url = new Uri("/MyFacebook.xaml", UriKind.Relative);
               NavigationService.Navigate(url);
           }

这是Api代码,并且在将OAuthExcepion投放到标有“Exception HERE”的行时崩溃了

    private static void ResponseCallback(IAsyncResult asyncResult, FacebookAsyncCallback callback, object state)
    {
        object result = null;
        FacebookApiException exception = null;
        try
        {
            var request = (HttpWebRequest)asyncResult.AsyncState;
            var response = (HttpWebResponse)request.EndGetResponse(asyncResult);

            using (Stream responseStream = response.GetResponseStream())
            {
                result = JsonSerializer.DeserializeObject(responseStream);
            }
        }
        catch (FacebookApiException)
        {
            // Rest API Errors
            throw;
        }
        catch (WebException ex)
        {
            // Graph API Errors or general web exceptions
         exception = ExceptionFactory.GetGraphException(ex);
            if (exception != null)
            {
                // Thow the FacebookApiException
                throw exception;
            }

            throw;  //Exception HERE
        }
        finally
        {
            // Check to make sure there hasn't been an exception.
            // If there has, we want to pass null to the callback.
            object data = null;
            if (exception == null)
            {
                data = result;
            }
            #if SILVERLIGHT
            callback(new FacebookAsyncResult(data, state, null, asyncResult.CompletedSynchronously, asyncResult.IsCompleted, exception));
            #else
            callback(new FacebookAsyncResult(data, state, asyncResult.AsyncWaitHandle, asyncResult.CompletedSynchronously, asyncResult.IsCompleted, exception));
           #endif
        }
    }

感谢

3 个答案:

答案 0 :(得分:1)

SDK的行为是有意的。异常不是“崩溃”应用程序,而是告诉您何时发生错误状态。你基本上是正确的,但不是捕捉异常,你应该只捕捉这样的FacebookOAuthException:

try {
    FacebookApp app = new FacebookApp(FacebookToken);
    var args = new Dictionary<string, object>();
    args["message"] = "hi";
    args["caption"] = "appcaption";
    args["description"] = "appdescription";
    args["name"] = "appname";
    args["picture"] = "apppicture";
    args["link"] = "applink";
    app.ApiAsync("/me/feed", args, (X) => { calback(X); }, HttpMethod.Post);
}
catch (FacebookOAuthException) {
    // This is where you would reauthorize the user or send them to a 'login' page
    Uri url = new Uri("/MyFacebook.xaml", UriKind.Relative);
    NavigationService.Navigate(url);
}

我还建议您阅读.Net异常处理,以便更好地了解它们的使用时间和原因。 http://msdn.microsoft.com/en-us/library/ms229005.aspx

答案 1 :(得分:0)

使用FacebookAuthenticationResult,您可以检查是否有错误,然后避免执行请求:

 private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookAuthenticationResult authResult;
        if (FacebookAuthenticationResult.TryParse(e.Uri, out authResult))
        {
            if (authResult.ErrorReason == "user_denied")
            {
                // Do something knowing that this failed (cancel).                   }
            else
            {
                fbApp.Session = authResult.ToSession();
                loginSucceeded(); 
            }                
        }
    }

答案 2 :(得分:-1)

你看过这篇文章吗? http://dotnetslackers.com/articles/net/wFace-windows-phone-7-facebook-integration-part-1.aspx

具体来说,请看第3部分 - http://dotnetslackers.com/articles/net/wp7Gesicht-windows-phone-7-facebook-integration-part-3.aspx

您是否可以存储访问令牌,然后在出错时再次显示登录页面?