Forum Xamarin.iOS

How to solve invalid_grant and invalid_request issue in Microsoft authorisation using Xamarin.Auth?

sumitmishrasumitmishra Member ✭✭✭
edited March 6 in Xamarin.iOS

Hi,
I am working on outlook rest Api for contact access in Xamarin.ios native app. I am using Xamarin.Auth library for authentication.
I have read Microsoft document for all setup like Azure Active Directory and configured as well. My iOS app open Microsoft authentication window but after entered login id and password it is sowing error as “one or more error: invalid_request and invalid_grant”.

Outlook Api reference link is:
https://docs.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/contacts-rest-operations

Azure Active Directory:
https://docs.microsoft.com/en-us/outlook/rest/

Please check my code and help me to solve this issue.

        public override void ViewDidLoad()
                {
                    base.ViewDidLoad();

                    AuthMsn = new OAuth2Authenticator(
                    clientId: "MyClientId",
                    scope: "https://outlook.office.com/contacts.read",
                    authorizeUrl:  new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/authorize"),
                    redirectUrl:  new Uri("msauth.com.MyApp.MsnContactDemo://auth"),
                    accessTokenUrl:  new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/token"),
                    clientSecret:"MyClientSecret"

                   );
                    BeginInvokeOnMainThread(() =>
                    {
                        var viewController = AuthMsn.GetUI();
                        PresentViewController(viewController, true, null);
                    });
                    AuthMsn.Completed += AuthMsn_Completed;
                    AuthMsn.Error += AuthMsn_Error;

                }
     private void AuthMsn_Completed(object sender, AuthenticatorCompletedEventArgs e)
            {
                if (e.IsAuthenticated)
                {
                }

            }
            private void AuthMsn_Error(object sender, AuthenticatorErrorEventArgs e)
            {

                  var a=  e.Exception;

            }

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to change the format of your redirectUrl:
    https://github.com/xamarin/Xamarin.Auth/issues/374

  • sumitmishrasumitmishra Member ✭✭✭
    edited March 10

    Hi @LandLu ,
    I have changed redirectUrl but it is showing error before entering password and my earlier redirectUrl that is mention above is working good but it is calling AuthMsn_Error event with invalid_grant and invalid_request error. Please check screen shot

  • sumitmishrasumitmishra Member ✭✭✭

    can you please tell me how to pass these two parameter using Xamarin.Auth

    code:<Code received from the previous call to /authorize endpoint of AAD>
    grant_type:authorization_code
    
Sign In or Register to comment.