Kiwi
Kiwi

Reputation: 2773

Getting token from Office 365 throws 'unknown_error'

I'm following the following tutorial(Outlook dev center tutorial) and my code is throwing an unknown_error in this part (at the get the token):

    public async Task<ActionResult> Authorize() {
        // Get the 'code' parameter from the Azure redirect
        string authCode = Request.Params["code"];

        string authority = "https://login.microsoftonline.com/common";
        string clientId = System.Configuration.ConfigurationManager.AppSettings["ida:ClientID"]; ;
        string clientSecret = System.Configuration.ConfigurationManager.AppSettings["ida:ClientSecret"]; ;
        AuthenticationContext authContext = new AuthenticationContext(authority);

        // The same url we specified in the auth code request
        Uri redirectUri = new Uri(Url.Action("Authorize", "Home", null, Request.Url.Scheme));

        // Use client ID and secret to establish app identity
        ClientCredential credential = new ClientCredential(clientId, clientSecret);

        try {
            // Get the token
            var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(authCode, redirectUri, credential, scopes); <--- HERE

            // Save the token in the session
            Session["access_token"] = authResult.Token;

            // Try to get user info
            Session["user_email"] = GetUserEmail(authContext, clientId);

            return Redirect(Url.Action("Inbox", "Home", null, Request.Url.Scheme));
        } catch (AdalException ex) {
            return Content(string.Format("ERROR retrieving token: {0}", ex.Message));
        }
    }

Stack trace:

at Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.AdalHttpClient.<GetResponseAsync>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.<SendHttpMessageAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.<SendTokenRequestAsync>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.<RunAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.<AcquireTokenByAuthorizationCodeCommonAsync>d__4a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.<AcquireTokenByAuthorizationCodeAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at OutlookCSharp.Controllers.HomeController.<Authorize>d__3.MoveNext() in C:\\_DevLocal\\OutlookApi\\OutlookCSharp\\Controllers\\HomeController.cs:line 61

anyone knows what might be going wrong?

Upvotes: 0

Views: 191

Answers (1)

Md. Alim Ul Karim
Md. Alim Ul Karim

Reputation: 2432

You can try this project , which already implemented the code from your referring url . And it can successfully login.

https://github.com/aukgit/Google-Outlook-Calender-Integrate/releases

There could be several things that could have gone wrong :

  • Probably you had missed to download the exact nuget packages
  • Install-Package Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory -Version 4.0.208020147-alpha -Pre
  • Install-Package Microsoft.Office365.OutlookServices-V2.0
  • Probably app is not registered correctly with url. enter image description here
  • Probably scopes are not given,

Upvotes: 1

Related Questions