Open tipa opened 5 years ago
@tipa could you please provide a small sample that demonstrates these issues so we can investigate please.
Regards
Some more code:
var auth = new OAuth1Authenticator(
consumerKey: _oAuthInfo.ConsumerKey,
consumerSecret: _oAuthInfo.ConsumerSecret,
requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"),
authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"),
accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"),
callbackUrl: new Uri("http://www.facebook.com/connect/login_success.html"));
activity.StartActivity(auth.GetUI(activity));
I understand the callback url is a bit weird, but it's at least one that correctly sends me back to the app after the user authorized the app
@tipa thanks for the update, i have assigned @moljac to investigate
Thanks! Crash also happens for Xamarin.iOS btw
I'm facing the same error. There's an update on this? Thanks
it's 2020 and the same error occured. any fix?
For anyone still encountering this problem today, I found a simple workaround is to inherit from OAuth1Authenticator and override the OnPageLoaded() method to catch the exception.
I choose to detect the specific error condition and surface this as a TaskCancelledException, but you could simply surface the 401 error directly if you prefer. The exception is then passed on to your auth.Error event callback as you would expect.
/// <summary>
/// A helper extension to Xamarin XAuth's OAuth1Authenticator to fix an issue with cancellation
/// https://github.com/xamarin/Xamarin.Auth/issues/323
/// </summary>
public class OAuth1AuthenticatorEx : OAuth1Authenticator
{
public OAuth1AuthenticatorEx(string consumerKey, string consumerSecret, Uri requestTokenUrl, Uri authorizeUrl, Uri accessTokenUrl, Uri callbackUrl, GetUsernameAsyncFunc getUsernameAsync = null, bool isUsingNativeUI = false)
: base(consumerKey, consumerSecret, requestTokenUrl, authorizeUrl, accessTokenUrl, callbackUrl, getUsernameAsync, isUsingNativeUI)
{
}
/// <summary>
/// An override to provide a workaround for OAuth1 throwing an uncaught (401) Authorization Required exception if the user cancels the login request
/// https://github.com/xamarin/Xamarin.Auth/issues/323
/// </summary>
/// <param name="url">The url of the page loaded by OAuth</param>
public override void OnPageLoaded(Uri url)
{
try
{
base.OnPageLoaded(url);
}
catch (Exception ex)
{
WebException wex = (WebException)ex.InnerException;
if (wex != null)
{
// Looks like the user cancelled the request?
HttpWebResponse response = (HttpWebResponse)wex.Response;
if (response != null && response.StatusCode == HttpStatusCode.Unauthorized)
{
OnError(new TaskCanceledException("Authorisation Cancelled")); // Or we could pass the inner exception directly
OnCancelled(); // Ensure the UI is closed
return;
}
}
OnError(ex); // Some other error, just pass it along
}
}
}
To use the fixed version, simply instantiate from the OAuth1AuthenticatorEx instead of the base OAuth1Authenticator. i.e.
var auth = new OAuth1AuthenticatorEx( consumerKey: _oAuthInfo.ConsumerKey, consumerSecret: _oAuthInfo.ConsumerSecret, ...
Xamarin.Auth Issue
Version
sample:
Steps to reproduce
Platform:
Expected behaviour
Exception should not be thrown. If instead of hitting Cancel you use phone back button it fires an Error event which is picked up in OnAuthError(). I think it should do the same.
Actual behaviour
Exception raised, see Stack Trace below.