使用Microsoft.Graph列出使用C#控制台应用程序

时间:2017-08-27 09:46:54

标签: c# console-application microsoft-graph

我正在这里找到一个真正的泡菜。 :(我只是不明白如何转移我在这里看到的内容:

https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/user_list_calendars

进入我的C#控制台应用程序的上下文。

我做了这么多:

//Set the scope for API call to user.read
string[] _scopes = new string[] { "user.read",  "calendars.read" };

并确认当我登录时确实要求访问用户日历。

但我不知道从哪里开始。我在示例代码中有这个:

   private void DisplayBasicTokenInfo(AuthenticationResult authResult)
    {
        string strTokenInfoText = "";
        if (authResult != null)
        {
            strTokenInfoText += $"Name: {authResult.User.Name}" + Environment.NewLine;
            strTokenInfoText += $"Username: {authResult.User.DisplayableId}" + Environment.NewLine;
            strTokenInfoText += $"Token Expires: {authResult.ExpiresOn.ToLocalTime()}" + Environment.NewLine;
            strTokenInfoText += $"Access Token: {authResult.AccessToken}" + Environment.NewLine;
            Console.WriteLine(strTokenInfoText);
        }
    }

但显然我没有什么可以用来进入日历列表。

我在这里问了一个类似的问题没有答案,但从那时起我取得了更多进展:

Using Microsoft Outlook API with C# to list calendars and add events

更新

我调整了我的方法:

public async Task AquireToken()
{
    AuthenticationResult authResult = null;

    try
    {
        if (authResult == null)
        {
            authResult = await Program.PublicClientApp.AcquireTokenSilentAsync(_scopes, Program.PublicClientApp.Users.FirstOrDefault());
        }
    }
    catch (MsalUiRequiredException ex)
    {
        // A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
        System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

        try
        {
            authResult = await Program.PublicClientApp.AcquireTokenAsync(_scopes);
        }
        catch (MsalException msalex)
        {
            strResultsText = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
        }
    }
    catch (Exception ex)
    {
        strResultsText = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
    }

    if (authResult != null)
    {
        strResultsText = await GetHttpContentWithToken(_graphAPIEndpoint, authResult.AccessToken);

        DisplayBasicTokenInfo(authResult);

        GraphServiceClient graphClient = new GraphServiceClient(
             new DelegateAuthenticationProvider(
                 (requestMessage) =>
                 {
                     // Append the access token to the request.
                     requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
                     return Task.FromResult(0);
                 }));

        SignOut();
        if (strResultsText != "")
            Console.WriteLine(strResultsText);
    }
}

如您所见,我添加了这段代码:

GraphServiceClient graphClient = new GraphServiceClient(
     new DelegateAuthenticationProvider(
         (requestMessage) =>
         {
             // Append the access token to the request.
             requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
             return Task.FromResult(0);
         }));

我在SignOut上设置了一个断点,但无法让它发挥作用。

我希望能够使用graphClient.me.Calendars

混淆。

更新

我试着看看我。也许我只是在错误的地方做事?但是当我提出一个断点时:

Breakpoint

这不像我期望的那样。

更新2

我调整了我的代码:

   if (authResult != null)
    {
        strResultsText = await GetHttpContentWithToken(_graphAPIEndpoint, authResult.AccessToken);

        DisplayBasicTokenInfo(authResult);

        GraphServiceClient graphClient = new GraphServiceClient(
                 new DelegateAuthenticationProvider(
                     (requestMessage) =>
                     {
                         // Append the access token to the request.
                         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
                         return Task.FromResult(0);
                     }));

        var calendars = await graphClient
                             .Me
                             .Calendars
                             .Request()
                             .GetAsync();
        Console.WriteLine(calendars.Count.ToString());
        List<Calendar> listCalendars = calendars.ToList();
        foreach(Calendar oCalendar in listCalendars)
        {
            Console.WriteLine(oCalendar.Name);
        }
        SignOut();
        if (strResultsText != "")
            Console.WriteLine(strResultsText);
    }

SignOut上有一个断点,即使监视列表中没有任何内容,但在我的控制台窗口中我有:

Console Output

所以似乎它运作正常。我想我只需要调整代码的逻辑并获得AquireToken方法来返回令牌并从那里获取它。我明天会玩更多。

更新3

感谢您的评论,我将其付诸实践:

public async Task BuildCalendarsList()
{
    if (_AuthResult == null)
        return;

    try
    {
        GraphServiceClient graphClient = new GraphServiceClient(
                 new DelegateAuthenticationProvider(
                     (requestMessage) =>
                     {
                         // Append the access token to the request.
                         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", _AuthResult.AccessToken);
                         return Task.FromResult(0);
                     }));

        var calendars = await graphClient
                             .Me
                             .Calendars
                             .Request()
                             .GetAsync();

        Console.WriteLine($"Number of calendars: {calendars.Count}");

        _ListCalendars = calendars.ToList();
    }
    catch(Exception ex)
    {
        _ListCalendars = null;
        Console.WriteLine($"Error BuildCalendarsList: {ex.Message}");
    }
}

public void DisplayCalendarsList()
{
    try
    {
        foreach (Calendar oCalendar in _ListCalendars)
        {
            string strCalendarInfo = $"Calendar Name: {oCalendar.Name}";
            strCalendarInfo += $" Calendar Id: {oCalendar.Id}";
            Console.WriteLine(strCalendarInfo);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine($"Error DisplayCalendarsList: {ex.Message}");
    }
 }

1 个答案:

答案 0 :(得分:0)

为了他人的利益,这就是我的工作方式:

public bool InitAuthenticatedClientAsync()
{
    bool bSuccess = true;

    _graphClient = new GraphServiceClient(
        new DelegateAuthenticationProvider(
            async (requestMessage) =>
            {
                try
                {
                    var accounts = await PublicClientApp.GetAccountsAsync();
                    // See: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/MSAL.NET-3-released#acquiring-a-token-also-got-simpler
                    _AuthResult = await PublicClientApp.AcquireTokenSilent(_Scopes, accounts.FirstOrDefault()).ExecuteAsync();
                }
                catch (MsalUiRequiredException ex)
                {
                    // A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
                    System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

                    try
                    {
                        // See: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/MSAL.NET-3-released#acquiring-a-token-also-got-simpler
                        _AuthResult = await PublicClientApp.AcquireTokenInteractive(_Scopes).ExecuteAsync();
                    }
                    catch (MsalException msalex)
                    {
                        SimpleLog.Log(msalex);
                        Console.WriteLine("GetAuthenticatedClientAsync: Acquire token error. See log.");
                        bSuccess = false;
                    }
                }
                catch (Exception ex)
                {
                    SimpleLog.Log(ex);
                    Console.WriteLine("GetAuthenticatedClientAsync: Acquire token silently error. See log.");
                    bSuccess = false;
                }

                if(bSuccess)
                {
                    // Append the access token to the request.
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", _AuthResult.AccessToken);

                    // Get event times in the current time zone.
                    requestMessage.Headers.Add("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\"");
                }
            }));

    return bSuccess;
}

public async Task<bool> BuildCalendarsList()
{
    bool bSuccess = true;

    if (_graphClient == null)
        return false;

    try
    {
        var calendars = await _graphClient
                             .Me
                             .Calendars
                             .Request()
                             .GetAsync();


        foreach (Calendar oCalendar in calendars)
        {
            _CalendarInfo.AddCalendarEntry(oCalendar.Name, oCalendar.Id);
        }

        bSuccess = SaveCalendarInfo();
    }
    catch (Exception ex)
    {
        SimpleLog.Log(ex);
        Console.WriteLine("BuildCalendarsList: See error log.");
        bSuccess = false;
    }

    return bSuccess;
}