{"id":1736,"date":"2022-08-30T15:19:01","date_gmt":"2022-08-30T15:19:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/29\/facebookoauthexception-was-unhandled-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:01","modified_gmt":"2022-08-30T15:19:01","slug":"facebookoauthexception-was-unhandled-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/facebookoauthexception-was-unhandled-collection-of-common-programming-errors\/","title":{"rendered":"FacebookOAuthException was unhandled-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m new in these things and I&#8217;ve been testing the api &#8230; and came to me a situation that is:<\/p>\n<p>if the user changes the password of Facebook<\/p>\n<p>the Access token is renewed &#8230; and try to post the API Launches an exception and the application crashes&#8230; how to resolve this situation?<\/p>\n<pre><code>         try {\n              FacebookApp app = new FacebookApp(FacebookToken);\n              var args = new Dictionary();\n              args[\"message\"] = \"hi\";\n              args[\"caption\"] = \"appcaption\";\n              args[\"description\"] = \"appdescription\";\n              args[\"name\"] = \"appname\";\n              args[\"picture\"] = \"apppicture\";\n              args[\"link\"] = \"applink\";\n              app.ApiAsync((X) =&gt; { calback(X); }, null, \"\/me\/feed\", args, HttpMethod.Post);\n          }\n           catch (Exception ex) {\n               Uri url = new Uri(\"\/MyFacebook.xaml\", UriKind.Relative);\n               NavigationService.Navigate(url);\n           }\n<\/code><\/pre>\n<p>this is Api code, and it&#8217;s crashing when throwing the OAuthExcepion at the line marked with &#8220;Exception HERE&#8221;<\/p>\n<pre><code>    private static void ResponseCallback(IAsyncResult asyncResult, FacebookAsyncCallback callback, object state)\n    {\n        object result = null;\n        FacebookApiException exception = null;\n        try\n        {\n            var request = (HttpWebRequest)asyncResult.AsyncState;\n            var response = (HttpWebResponse)request.EndGetResponse(asyncResult);\n\n            using (Stream responseStream = response.GetResponseStream())\n            {\n                result = JsonSerializer.DeserializeObject(responseStream);\n            }\n        }\n        catch (FacebookApiException)\n        {\n            \/\/ Rest API Errors\n            throw;\n        }\n        catch (WebException ex)\n        {\n            \/\/ Graph API Errors or general web exceptions\n         exception = ExceptionFactory.GetGraphException(ex);\n            if (exception != null)\n            {\n                \/\/ Thow the FacebookApiException\n                throw exception;\n            }\n\n            throw;  \/\/Exception HERE\n        }\n        finally\n        {\n            \/\/ Check to make sure there hasn't been an exception.\n            \/\/ If there has, we want to pass null to the callback.\n            object data = null;\n            if (exception == null)\n            {\n                data = result;\n            }\n            #if SILVERLIGHT\n            callback(new FacebookAsyncResult(data, state, null, asyncResult.CompletedSynchronously, asyncResult.IsCompleted, exception));\n            #else\n            callback(new FacebookAsyncResult(data, state, asyncResult.AsyncWaitHandle, asyncResult.CompletedSynchronously, asyncResult.IsCompleted, exception));\n           #endif\n        }\n    }\n<\/code><\/pre>\n<p>thanks<\/p>\n<ol>\n<li>\n<p>The behavior of the SDK is intended. An exception is not &#8220;crashing&#8221; 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:<\/p>\n<pre><code>try {\n    FacebookApp app = new FacebookApp(FacebookToken);\n    var args = new Dictionary();\n    args[\"message\"] = \"hi\";\n    args[\"caption\"] = \"appcaption\";\n    args[\"description\"] = \"appdescription\";\n    args[\"name\"] = \"appname\";\n    args[\"picture\"] = \"apppicture\";\n    args[\"link\"] = \"applink\";\n    app.ApiAsync(\"\/me\/feed\", args, (X) =&gt; { calback(X); }, HttpMethod.Post);\n}\ncatch (FacebookOAuthException) {\n    \/\/ This is where you would reauthorize the user or send them to a 'login' page\n    Uri url = new Uri(\"\/MyFacebook.xaml\", UriKind.Relative);\n    NavigationService.Navigate(url);\n}\n<\/code><\/pre>\n<p>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<\/p>\n<\/li>\n<li>\n<p>Using the FacebookAuthenticationResult you can check if there was an error and then avoid to do the request:<\/p>\n<pre><code> private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)\n    {\n        FacebookAuthenticationResult authResult;\n        if (FacebookAuthenticationResult.TryParse(e.Uri, out authResult))\n        {\n            if (authResult.ErrorReason == \"user_denied\")\n            {\n                \/\/ Do something knowing that this failed (cancel).                   }\n            else\n            {\n                fbApp.Session = authResult.ToSession();\n                loginSucceeded(); \n            }                \n        }\n    }\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-29 20:00:32. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m new in these things and I&#8217;ve been testing the api &#8230; and came to me a situation that is: if the user changes the password of Facebook the Access token is renewed &#8230; and try to post the API Launches an exception and the application crashes&#8230; how to resolve this situation? try { FacebookApp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1736","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1736"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1736\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}