【问题标题】:Getting token from Office 365 throws 'unknown_error'从 Office 365 获取令牌会引发“unknown_error”
【发布时间】:2016-05-17 17:39:44
【问题描述】:

我正在关注以下教程(Outlook dev center tutorial),我的代码在这部分(在获取令牌时)抛出了一个unknown_error

    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));
        }
    }

堆栈跟踪:

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

有人知道可能出了什么问题吗?

【问题讨论】:

  • 您使用的是什么版本的 ADAL 库?您可以在 Visual Studio 的包管理器控制台中运行 get-package Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory 以获取版本。
  • @JasonJohnston:版本是:4.0.208052020-alpha
  • 好的,我使用的是同一个版本,没有问题。 catch (AdalException ex) 行是否捕获了此错误?那里有更多信息吗?
  • @JasonJohnston 错误确实被 AdalException 捕获,没有其他信息可用:/

标签: outlook office365 adal office365api


【解决方案1】:

你可以试试这个项目,它已经实现了来自你的引用 url 的代码。并且可以成功登录。

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

可能有几件事可能出错:

  • 可能您错过了下载确切的 nuget 包
  • 安装包 Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory -Version 4.0.208020147-alpha -Pre
  • 安装包 Microsoft.Office365.OutlookServices-V2.0
  • 可能应用程序未正确注册到 url。
  • 可能没有给出范围,

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多