开发者

FacebookOAuthException was unhandled

I'm new in these things and I've been testing the api ... and came to me a situation that is:

if the user changes the password of Facebook

the Access token is renewed ... and try to post the API Launches an exception and the application crashes... how to resolve this situation?

         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);
           }

this is Api code, and it's crashing when throwing the OAuthExcepion at the line marked with "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
        }
    }

thanks


The behavior of the SDK is intended. An exception is not "crashing" the application, but rather telling you when an error state has occurred. You are basically doing it correctly, but instead of catching Exception you should only catch FacebookOAuthException like this:

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);
}

I would also suggest reading up on .Net exception handling to give you a better understanding of when and why they are used. http://msdn.microsoft.com/en-us/library/ms229005.aspx


Using the FacebookAuthenticationResult you can check if there was an error and then avoid to do the request:

 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(); 
            }                
        }
    }


Have you seen this post? http://dotnetslackers.com/articles/net/wFace-windows-phone-7-facebook-integration-part-1.aspx

Specifically, take a look at part 3 - http://dotnetslackers.com/articles/net/wp7Gesicht-windows-phone-7-facebook-integration-part-3.aspx

Could you store the access token, and then on error, show the login page again?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜