/// <summary> /// Exchange code for access token asynchronously. /// </summary> /// <param name="facebookOAuthClient">The Facebook OAuth Client.</param> /// <param name="code">The code.</param> /// <param name="parameters">The parameters.</param> /// <returns>The task of the result.</returns> public static Task <object> ExchangeCodeForAccessTokenTaskAsync(this FacebookOAuthClient facebookOAuthClient, string code, IDictionary <string, object> parameters) { var tcs = FacebookClientTaskExtensions.CreateSource <object>(null); EventHandler <FacebookApiEventArgs> handler = null; handler = (sender, e) => FacebookClientTaskExtensions.TransferCompletionToTask <object>(tcs, e, () => e.GetResultData(), () => facebookOAuthClient.ExchangeCodeForAccessTokenCompleted -= handler); facebookOAuthClient.GetApplicationAccessTokenCompleted += handler; try { facebookOAuthClient.ExchangeCodeForAccessTokenAsync(code, parameters, tcs); } catch { facebookOAuthClient.ExchangeCodeForAccessTokenCompleted -= handler; tcs.TrySetCanceled(); throw; } return(tcs.Task); }