Forum Xamarin.Forms

ViewController doesn’t disappear after logging in (Instagram)

PiratePirate SAMember ✭✭

Hi ,
I used this sample to authenticate using Instagram :
https://github.com/CrossGeeks/SocialMediaAuthenticationSample

There is one issue though , after logging in with Instagram even though it’s set to dismiss the viewcontroller but it doesn’t disappear after logging in . The only option is to cancel which does reverse of what I need here.

Any reason why ?

Best Answer

  • PiratePirate SAMember ✭✭
    Accepted Answer

    Thanks y_SBSIC for pointing out the issue . It's the redirect uri . I used this one and worked well .
    https://api.instagram.com/oauth/authorize/?client_id=YOURCLIENTID&redirect_uri=YOURREDIRECTURI&response_type=code

Answers

  • Ajay_SBSICAjay_SBSIC USMember ✭✭✭

    Check your redirect URL and compare your code with below :

        public class OAuth2Service : IOAuth2Service
        {
            public event EventHandler<string> OnSuccess = delegate { };
            public event EventHandler OnCancel = delegate { };
            public event EventHandler<string> OnError = delegate { };
    
            public void Authenticate(string clientId, string scope, Uri authorizeUrl, Uri redirectUrl)
            {
    
                var window = UIApplication.SharedApplication.KeyWindow;
                var vc = window.RootViewController;
                while (vc.PresentedViewController != null)
                {
                    vc = vc.PresentedViewController;
                }
    
                var auth = new iOSOAuth2Authenticator(
                    clientId: clientId, // your OAuth2 client id
                    scope: scope, // the scopes for the particular API you're accessing, delimited by "+" symbols
                    authorizeUrl: authorizeUrl, // the auth URL for the service
                    redirectUrl: redirectUrl); // the redirect URL for the service
    
                auth.AllowCancel = true;
                auth.ShowErrors = false;
                EventHandler<AuthenticatorErrorEventArgs> errorDelegate = null;
                EventHandler<AuthenticatorCompletedEventArgs> completedDelegate = null;
    
                errorDelegate = (sender, eventArgs) =>
                {
                    OnError?.Invoke(this, eventArgs.Message);
    
                    auth.Error -= errorDelegate;
                    auth.Completed -= completedDelegate;
                };
    
                completedDelegate = (sender, eventArgs) => {
    
    
    
                    // UI presented, so it's up to us to dimiss it on iOS
                    // dismiss ViewController with UIWebView or SFSafariViewController
                    vc.DismissViewController(true, null);
    
    
                    if (eventArgs.IsAuthenticated)
                    {
    
                        OnSuccess?.Invoke(this, eventArgs.Account.Properties["access_token"]);
    
                    }
                    else
                    {
                        // The user cancelled
    
                        OnCancel?.Invoke(this, EventArgs.Empty);
                    }
                    auth.Error -= errorDelegate;
                    auth.Completed -= completedDelegate;
    
                };
    
                auth.Error += errorDelegate;
                auth.Completed += completedDelegate;
    
                vc.PresentViewController(auth.GetUI(), true, null);
            }
    
        }
    

    You can check here :
    var window = UIApplication.SharedApplication.KeyWindow;
    var vc = window.RootViewController;
    and
    vc.DismissViewController(true, null);

  • Ajay_SBSICAjay_SBSIC USMember ✭✭✭

    Post your sample here if still facing any issue.

  • PiratePirate SAMember ✭✭

    Dude , I have exactly the same code which doesn't work as I said . Redirect URL is set to :

    const string InstagramRedirectUrl = "https://localhost:3000/callback";

    That doesn't work also .

  • PiratePirate SAMember ✭✭
    Accepted Answer

    Thanks y_SBSIC for pointing out the issue . It's the redirect uri . I used this one and worked well .
    https://api.instagram.com/oauth/authorize/?client_id=YOURCLIENTID&redirect_uri=YOURREDIRECTURI&response_type=code

Sign In or Register to comment.