繁体   English   中英

如何在 Asp.net 核心 web api 中使用图形 Api 连接到 Outlook 日历

[英]how to connect to outlook calendar using graph Api in Asp.net core web api

我创建了 asp.net 核心 web api 应用程序来连接到 Microsoft Outlook 日历并使用图形 api 添加新事件。 我已经创建了 Microsoft Office 365 E3 商业帐户(试用版)。 我已使用同一帐户在 Azure 门户的活动目录中注册了我的应用程序。 如下图所示设置 API 权限。 [![在此处输入图像描述][1]][1]

我的身份验证类是

    using System.Net.Http;
using System.Net.Http.Headers;
using System.Security;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Graph;

namespace Helpers
{
    public class MsalAuthenticationProvider : IAuthenticationProvider
    {
        private static MsalAuthenticationProvider _singleton;
        private IPublicClientApplication _clientApplication;
        private string[] _scopes;
        private string _username;
        private SecureString _password;
        private string _userId;

        private MsalAuthenticationProvider(IPublicClientApplication clientApplication, string[] scopes, string username, SecureString password)
        {
            _clientApplication = clientApplication;
            _scopes = scopes;
            _username = username;
            _password = password;
            _userId = null;
        }

        public static MsalAuthenticationProvider GetInstance(IPublicClientApplication clientApplication, string[] scopes, string username, SecureString password)
        {
            if (_singleton == null)
            {
                _singleton = new MsalAuthenticationProvider(clientApplication, scopes, username, password);
            }

            return _singleton;
        }

        public async Task AuthenticateRequestAsync(HttpRequestMessage request)
        {
            var accessToken = await GetTokenAsync();

            request.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
        }

        public async Task<string> GetTokenAsync()
        {
            if (!string.IsNullOrEmpty(_userId))
            {
                try
                {
                    var account = await _clientApplication.GetAccountAsync(_userId);

                    if (account != null)
                    {
                        var silentResult = await _clientApplication.AcquireTokenSilent(_scopes, account).ExecuteAsync();
                        return silentResult.AccessToken;
                    }
                }
                catch (MsalUiRequiredException) { }
            }

            var result = await _clientApplication.AcquireTokenByUsernamePassword(_scopes, _username, _password).ExecuteAsync();
            _userId = result.Account.HomeAccountId.Identifier;
            return result.AccessToken;
        }
    }
}

我的控制器是

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Versioning;
using System.Security;
using System.Threading.Tasks;
using Helpers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using WebAPI_OneDrive.Models;

namespace WebAPI_OneDrive.Controllers
{
    //[Route("api/[controller]")]
    [ApiController]
    public class OutlookController : ControllerBase
    {
        [HttpPost]
        [Route("Outlook/CreateEvent")]
        public async Task<string> CreateEventAsync(/*CredentialsModel model*/)
        {
            CredentialsModel model = new CredentialsModel();

            model.applicationId = "9403804b-f9b6-4bb0-b8dd-2f40b4347ef1";
            model.tenantId = "926349b9-42cb-46c0-bb34-970256de5c41";
            model.userName = "alibaba@alibaba123456.onmicrosoft.com";
            model.password = "p@ssw0rd";

            var userName = model.userName;
            var stringPassword = model.password;
            var applicationId = model.applicationId;
            var tenantId = model.tenantId;
            var userPassword = new SecureString();
            foreach (char c in stringPassword)
            {
                userPassword.AppendChar(c);
            }

            var config = LoadAppSettings(applicationId, tenantId);

            var graphClient = GetAuthenticatedGraphClient(config, userName, userPassword);


            //Create Event

            var @event = new Event
            {
                Subject = "Let's go for lunch",
                Body = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content = "Does noon work for you?"
                },
                Start = new DateTimeTimeZone
                {
                    DateTime = "2022-07-19T12:00:00",
                    TimeZone = "Pacific Standard Time"
                },
                End = new DateTimeTimeZone
                {
                    DateTime = "2022-07-19T14:00:00",
                    TimeZone = "Pacific Standard Time"
                },
                Location = new Location
                {
                    DisplayName = "Harry's Bar"
                },
                Attendees = new List<Attendee>()
    {
        new Attendee
        {
            EmailAddress = new EmailAddress
            {
                Address = "samanthab@contoso.onmicrosoft.com",
                Name = "Samantha Booth"
            },
            Type = AttendeeType.Required
        }
    },
                AllowNewTimeProposals = true,
                TransactionId = "7E163156-7762-4BEB-A1C6-729EA81755A7"
            };


            var Event = await graphClient.Me.Calendar.Events
    .Request()
    .AddAsync(@event);
            var EventId = Event.Id;
            return EventId;
        }
        private static IConfigurationRoot LoadAppSettings(string applicationId, string tenantId)
        {
            try
            {
                var config = new ConfigurationBuilder()
                                  .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                                  .AddJsonFile("appsettings.json", false, true)
                                  .Build();
                config["applicationId"] = applicationId;
                config["tenantId"] = tenantId;
                if (string.IsNullOrEmpty(config["applicationId"]) ||
                    string.IsNullOrEmpty(config["tenantId"]))
                {
                    return null;
                }

                return config;
            }
            catch (System.IO.FileNotFoundException)
            {
                return null;
            }
        }

        private static IAuthenticationProvider CreateAuthorizationProvider(IConfigurationRoot config, string userName, SecureString userPassword)
        {
            var clientId = config["applicationId"];
            var authority = $"https://login.microsoftonline.com/{config["tenantId"]}/v2.0";

            List<string> scopes = new List<string>();
            scopes.Add("User.Read");
            scopes.Add("Calendars.Read");
            scopes.Add("Calendars.Read.Shared");
            scopes.Add("Calendars.ReadWrite");
            scopes.Add("Calendars.Read.Shared");
            //scopes.Add("Files.Read");
            //scopes.Add("Files.ReadWrite");
            //scopes.Add("Files.Read.All");
            //scopes.Add("Files.ReadWrite.All");
            //scopes.Add("Files.Read.Selected");
            //scopes.Add("Files.ReadWrite.Selected");
            //scopes.Add("Files.ReadWrite.AppFolder");
            var cca = PublicClientApplicationBuilder.Create(clientId)
                                                    .WithAuthority(authority)
                                                    .Build();
            return MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray(), userName, userPassword);
        }

        private static GraphServiceClient GetAuthenticatedGraphClient(IConfigurationRoot config, string userName, SecureString userPassword)
        {
            var authenticationProvider = CreateAuthorizationProvider(config, userName, userPassword);
            var graphClient = new GraphServiceClient(authenticationProvider);
            return graphClient;
        }



        //Model

        public class CredentialsModel
        {
            ///credentials
            public string password { get; set; }
            public string userName { get; set; }
            public string applicationId { get; set; }
            public string tenantId { get; set; }
        }
    }
}

当我调用 Outlook/CreateEvent 时,我收到以下错误 [![在此处输入图像描述][2]][2]

请指导我如何修复此错误,在此先感谢 [1]: https ://i.stack.imgur.com/PjLQF.png [2]: https ://i.stack.imgur.com/6MK2N.png

要解决此问题,请尝试如下授予应用程序权限并授予管理员同意:

在此处输入图像描述

添加应用程序权限后,尝试获取承载令牌,如下所示

在此处输入图像描述

请检查此参考列表事件 - Microsoft Graph v1.0 | Microsoft Docs as per document最后,我成功获取了日历事件列表:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM