使用范围“https://outlook.com/Calendars.ReadWrite”时返回空标记

时间:2015-11-17 12:42:26

标签: office365 office365api outlook-restapi

我正在尝试编写一个在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));
        }
    }

0 个答案:

没有答案