Xamarin.Auth and infinite error alerts

I'm testing login using Xamarin.Auth and OAuth2Authenticator, if I disable the internet connection then the Error event is raised, but there's also an endless number of Alert windows that popup telling me there is no internet connection. Each time you click "ok", the alert just pops up again, over and over, until you force quit the app.

When the Error event is raised I dismiss the UIPopoverController that is presenting the login window and remove the event handlers, but this doesn't stop the authenticator from trying to connect over and over and over again. There doesn't seem to be a cancel method (calling OnCancel() does nothing), what is the correct way to handle this error?

Posts

  • JustinMosesJustinMoses Justin Moses USBeta

    Yeah, I'm having serious problems with Xamarin.Auth. Mostly, in regards to Google API requests. The only way I can get a code, is to use the redirect uri of "urn:ietf:wg:oauth:2.0:oob", and the code will show up, but the component doesn't recognize that authentication has been successful, and consequently even in cancelling the request, there is no real way to handle the problem. Otherwise, it does have a tendency to spam errors, and often.

  • JustinMosesJustinMoses Justin Moses USBeta

    Actually, Just dove into the Xamarin.Auth library, and found where the problem is. The Google response is in JSON, and the Xamarin Auth library expects a form response.

    It's on line 316 of Xamarin.Auth in OAuth2Authenticator.

  • ZacharyGramanaZacharyGramana Zachary Gramana USXamarin Team Xamurai

    FWIW, @justinmoses, I'm finding that Xam.Auth works best when you give it a redirect URI with an http/https schema.

  • JustinMosesJustinMoses Justin Moses USBeta

    That isn't the problem. The problem is that Xamarin.Auth is setup for Web Redirects, as in web projects. It also doesn't support JSON requests, which I fixed in my local copy of the repo.

    Right now, I have Xamarin.Auth working on my project (I am authenticating), with a single exception pop up, when it tries to redirect to the redirect url (localhost, which of course fails). I'm working on extracting the code from the other redirect option (which is more for "Mobile"), "urn:ietf:wg:oauth:2.0:oob". Which places the access_token into a web page text field.

    Just going to go tweak it a bit more and get past the pain. : )

    Then I should be up and going. Can always get the update to Xamarin.Auth later.

  • JustinMosesJustinMoses Justin Moses USBeta

    Yeah, got it working. Both Xamarin.Android, and Xamarin.IOS. Man, that was a tussle.

  • DaveWDaveW David Wolfe USMember

    Yeah I had to edit the source code too. For some reason WebAuthenticatorController.keepTryingAfterError wasn't ever changing to false. I didn't have time to figure out the problem, I just set it to false and now I only get one error popup.

  • ZacharyGramanaZacharyGramana Zachary Gramana USXamarin Team Xamurai

    One issue people may see is the "infinite" NSUrlDomainError alert box. Make sure that you are using the iPhone Simulator 6.x, not 5.0.

  • JensRatseyJensRatsey Jens Ratsey US

    Also having issues with Xamarin.Auth. Works great for Facebook, but no dice for Google. Using

                    var gAuth = new OAuth2Authenticator(
    
                        "xxxxxxxxxxxxxxx.apps.googleusercontent.com",
    
                        "openid email",
    
                        new Uri("https://accounts.google.com/o/oauth2/auth"),
    
                        new Uri("http://localhost"),
    
                        null);
    

    I just get a response 'unsupported_response_type' after entering my Google credentials. I loaded up the Xamarin.Auth.Android project and see that it's because GetInitialUrlAsync() is setting the response_type parameter to 'token' due to IsImplicit function returning true. This in turn due to there not being an accessTokenUrl passed in.

    That accessTokenUrl is only in the constructor that takes a secret, which is not for mobile apps. According to the Google API docs the response_code parameter:

    Determines if the Google OAuth 2.0 endpoint returns an authorization code. For installed applications, a value of code should be used.

    I tried forcing GetInitialUrlAsync to use "code". This then successfully passes the credentials and presents me a screen asking whether my app should have access which I accept. OnRedirectPageLoaded is then called to parse the code that Google has given me, which is embedded in the URL returned to http://localhost. This now fails since it's again calling IsImplicit to decide to look for a code. Since that function returns true, it's expecting a token and fails with 'Expected access_token in response, but did not receive one.'

    So, next step is to change the check against IsImplicit in OnRedirectPageLoaded to look for the code if the function is true.

    RequestAccessTokenAsync now fails because the accessTokenUrl has not been set. So I figure it doesn't like the http://localhost as return URL. I changed it to the urn:xx:xx:xx:oath:2.0:oob address and it then seems to work - to some extent. It presents me with a page showing the code that I should 'copy, switch to my app, and paste'.

    I haven't been able to do that yet, but even if I could, clearly that's not a seamless authentication process and there must be a better way. Am I doing it wrong, or does Xamarin.Auth just not work yet for Android mobile apps authenticating against Google?

  • DaveWDaveW David Wolfe USMember

    JensR, is your client id for web applications or installed applications? I could only get Xamarin.Auth to work with Google if I used a client id for web applications, even though I'm not making a web app. The redirect uri is just our dev site, as soon as the user is logged in we close the login ui so the user never sees the redirect page.

  • EricMaupinEricMaupin Eric Maupin USXamarin Team Xamurai

    RequestAccessTokenAsync now fails because the accessTokenUrl has not been set. So I figure it doesn't like the http://localhost as return URL. I changed it to the urn:xx:xx:xx:oath:2.0:oob address and it then seems to work - to some extent. It presents me with a page showing the code that I should 'copy, switch to my app, and paste'.

    Xamarin.Auth does not support using localhost or the urn: method, you'll need to use an actual web page.

  • ZacharyGramanaZacharyGramana Zachary Gramana USXamarin Team Xamurai

    You can also use a custom URI scheme for the callback URI. For some OAuth providers, like Salesforce, this is the only reliable way to get a refresh_token.

    The caveat here is that you must register your custom URI scheme in your app's info.plist to avoid errors from UIWebView. Take a look at the Salesforce component's sample iOS app for an example.

  • JensRatseyJensRatsey Jens Ratsey US

    So my original question seems to be answered with 'No, Xamarin.Auth does not work with Google for mobile apps.'

    @Dave: I'll take a look at your method. My app will have a companion web site, so that might be an effective solution for me.

    @Zack: Will take a look there too, although I'm targeting Android so will have to be conscious of any platform differences.

    @Eric: It was so easy to get Facebook authentication working I had assumed I had done something wrong in trying to hook up Google. Are there plans to support Google authentication using localhost? I do see that a call is returned with the token in the URL, so it seems like it should be possible.

  • GaborFurediGaborFuredi Gabor Furedi HUMember ✭✭

    Has this been sorted out yet?

  • tohweitohwei Jeff Lim USMember ✭✭

    Hi there,

    I have a question about how to get 'Facebook publish_action' approved before publishing.
    As far as what I understand, U need to get the approval from Facebook for 'Publish_Action' if your app (in Facebook) created after April 30th 2014.

    I have done the Facebook Auth using Xamarin.Auth component. My app will allow user to share something using Publish_action (post to feed).

    So, my app will first ask for the permission from the end user to allow me to post something on behalf of them. But, in order to do that, I have to get permissions/approval from Facebook for the 'Publish_action'.

    However, I have no idea how to configure the settings in Facebook developer's app section.
    The main issue or doubt about how to configure or create an Facebook app are:

    Q1. I wish to get the Publish_action approval first before I publish my iOS app to AppStore. Because I want to ensure the function is working and tested before to AppStore. How to achieve this? How possible to get the Approval from Facebook first before to AppStore? Particularly on how to write the step-by-step instruction in Facebook?

    Q2: Anyone done something similar? Please share your experience on how to create APP in Facebook for your mobile APP through the development with Xamarin.

    /Jeff

  • SuchithSuchith Suchith M INMember ✭✭
  • CodyCody Cody USMember ✭✭

    Hello,

    Has anyone fixed this issue?

    https://forums.xamarin.com/discussion/50793/xamarin-auth-log-in#latest

    Thank you for the help!

  • NamHoang.3859NamHoang.3859 Nam Hoang CAMember ✭✭

    @ZacharyGramana said:
    One issue people may see is the "infinite" NSUrlDomainError alert box. Make sure that you are using the iPhone Simulator 6.x, not 5.0.

    @ZacharyGramana: The problem still persists on both simulators. I am using Xamarin.Auth assembly version 1.2.2.0.

    @DaveW said:
    I'm testing login using Xamarin.Auth and OAuth2Authenticator, if I disable the internet connection then the Error event is raised, but there's also an endless number of Alert windows that popup telling me there is no internet connection. Each time you click "ok", the alert just pops up again, over and over, until you force quit the app.

    @DaveW: Can you please give me some details of your workaround for the problem? I am having exactly the same issue.

  • TranTienDat.VnTranTienDat.Vn Tran Tien Dat USMember ✭✭

    I have same issue too. I login by company's authenticator. Although i login successful, it's always return error (error 400 and 500) until i logoff or quit application.
    Because my redirectUri is not exists (also using http/https schema)

    I found a solution. Implement new class XXXAuth inherit from OAuth2Authenticator
    Add new property IsEnabled

    public bool IsEnabled { get; set; } = true;
    

    override these method

    public override void OnPageLoaded(Uri url)
    {
        if (IsEnabled)
        {
            base.OnPageLoaded(url);
            Console.WriteLine("OnPageLoaded : " + url.AbsoluteUri);
        }
    }
    
    public override void OnPageLoading(Uri url)
    {
        if (IsEnabled)
        {
            base.OnPageLoading(url);
            Console.WriteLine("OnPageLoading");
        }
    }
    
    protected override void OnPageEncountered(Uri url, IDictionary<string, string> query, IDictionary<string, string> fragment)
    {
        if (IsEnabled)
        {
            base.OnPageEncountered(url, query, fragment);
            Console.WriteLine("OnPageEncountered");
        }
    }
    

    And set IsEnabled = false in OnError event

    auth.Error += (object sender, AuthenticatorErrorEventArgs eventArgs) => {
        auth.IsEnabled = false;
    };
    

    It won't show error anymore !

  • Avi669Avi669 Avinash vavhale INMember ✭✭

    Xamarin.Auth does not support using localhost or the actual web page in redirect URL for google OAuth, But when i use urn:ietf:wg:oauth:2.0:oob i'm getting access token on next page.
    Any one found workaround for this?

    Thanks in Advance!

  • JohnHardmanJohnHardman John Hardman GBUniversity ✭✭✭✭✭
    edited March 3

    Has anybody got a solution to the infinite alert box problem? I am seeing this on physical Android and iOS devices.

    [Answered my own question] There is a ShowErrors property on the OAuth2Authenticator that can be set to false. It doesn't fix the underlying problem, but it hides it.

  • NickKovalskyNickKovalsky Nick Kovalsky USMember ✭✭
    edited August 30

    Aha, @JohnHardman call OnCancel() inside your authenticator Error handler and this nightmare will end. :)

            //-------------------------------------------------------
            public async void OnAuthError(object sender, AuthenticatorErrorEventArgs e)
            //-------------------------------------------------------
            {
                var authenticator = sender as OAuth2Authenticator;
                if (authenticator != null)
                {
                    authenticator.Completed -= OnAuthComplete;
                    authenticator.Error -= OnAuthError;
                }
    
                authenticator.OnCancelled(); //calm down
    
                Debug.WriteLine("External Authentication error: " + e.Message);
                await CloseExternalLoginPage(); // basically PopModalAsync
            }
    
Sign In or Register to comment.