2

我正在尝试编写一个在 Office 365 中自动添加/删除/更新事件(日历)的应用程序。

我遵循了本教程:https ://dev.outlook.com/RestGettingStarted/Tutorial/dotnet 。通过这种方式,我设法让应用程序阅读邮件和阅读事件。

但是当我尝试添加范围“” https://outlook.office.com/Calendars.ReadWrite以便我可以添加事件时,我似乎无法获得有效的授权令牌。

当我使用断点时,我看到 authResult.Token 的值为 null。如何获得合适的令牌?

public class HomeController : Controller
{
    // The required scopes for our app
    // "https://outlook.office.com/calendars.read",
    // "https://outlook.office.com/Calendars.ReadWrite"
    // "https://outlook.office.com/mail.read",
    private static string[] scopes = { "https://outlook.office.com/mail.read",
            "https://outlook.office.com/Calendars.ReadWrite" };

    public async Task<ActionResult> SignIn()
    {
        string authority = "https://login.microsoftonline.com/common";
        string clientId = System.Configuration.ConfigurationManager.AppSettings["ida:ClientID"];
        AuthenticationContext authContext = new AuthenticationContext(authority);

        // The url in our app that Azure should redirect to after successful signin
        Uri redirectUri = new Uri(Url.Action("Authorize", "Home", null, Request.Url.Scheme));

        // Generate the parameterized URL for Azure signin
        Uri authUri = await authContext.GetAuthorizationRequestUrlAsync(scopes, null, clientId,
            redirectUri, UserIdentifier.AnyUser, null);

        // Redirect the browser to the Azure signin page
        return Redirect(authUri.ToString());
    }

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

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

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

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

0 回答 0