I'm getting error that DelegateAuthenticationProvider and MeRequestBuilder are not found in the graphService client I use to read Microsoft 365 mail

huangapple go评论73阅读模式
英文:

I'm getting error that DelegateAuthenticationProvider and MeRequestBuilder are not found in the graphService client I use to read Microsoft 365 mail

问题

自从Microsoft在2023年将基本认证切换为令牌认证后,我无法执行邮件阅读操作。我正在尝试使用GraphServiceClient进行阅读,但我遇到了下面提到的错误,您能帮忙吗?

  • CS0246-->找不到类型或命名空间名称'DelegateAuthenticationProvider'(您是否缺少using指令或程序集引用?)
  • CS1061-->'MeRequestBuilder'不包含对'Request'的定义,也找不到接受类型为'MeRequestBuilder'的第一个参数的可访问扩展方法'Request'(您是否缺少using指令或程序集引用?)
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http.Headers;
using Microsoft.Identity.Client;
namespace GraphTest.Helpers
{
    public class GraphHelper
    {
        public static async Task<CachedUser> GetUserDetailsAsync(string accessToken)
        {
            var graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        requestMessage.Headers.Authorization =
                            new AuthenticationHeaderValue("Bearer", accessToken);
                    }));

            var user = await graphClient.Me.Request()
                .Select(u => new
                {
                    u.DisplayName,
                    u.Mail,
                    u.UserPrincipalName
                })
                .GetAsync();

            return new CachedUser
            {
                Avatar = string.Empty,
                DisplayName = user.DisplayName,
                Email = string.IsNullOrEmpty(user.Mail) ?
                    user.UserPrincipalName : user.Mail
            };  
        }  
    }  
}
英文:

Since Microsoft has switched from basic auth to bearer auth as of 2023, I cannot perform mail reading operations. I am trying to read with GraphServiceClient, but I am getting the below mentioned errors,can you help?

*- CS0246-->The type or namespace name 'DelegateAuthenticationProvider' could not be found (are you missing a using directive or an assembly reference?)

  • CS1061--> 'MeRequestBuilder' does not contain a definition for 'Request' and no accessible extension method 'Request' accepting a first argument of type 'MeRequestBuilder' could be found (are you missing a using directive or an assembly reference?)*
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http.Headers;
using Microsoft.Identity.Client;
namespace GraphTest.Helpers
{
    public class GraphHelper
    {
        public static async Task<CachedUser> GetUserDetailsAsync(string accessToken)
        {
            var graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        requestMessage.Headers.Authorization =
                            new AuthenticationHeaderValue("Bearer", accessToken);
                    }));

            var user = await graphClient.Me.Request()
                .Select(u => new
                {
                    u.DisplayName,
                    u.Mail,
                    u.UserPrincipalName
                })
                .GetAsync();

            return new CachedUser
            {
                Avatar = string.Empty,
                DisplayName = user.DisplayName,
                Email = string.IsNullOrEmpty(user.Mail) ?
                    user.UserPrincipalName : user.Mail
            };  
        }  
    }  
}

答案1

得分: 2

以下是翻译的部分:

v5 SDK有很多变化,您可以在此处了解更多信息:

https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md

身份验证

在DelegateAuthenticationProvider的位置,可以通过创建IAccessTokenProvider的实现并与来自Kiota抽象的BaseBearerTokenAuthenticationProvider一起使用来执行自定义身份验证流程,如下所示:

public class TokenProvider : IAccessTokenProvider
{
    public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = default,
        CancellationToken cancellationToken = default)
    {
        var token = "token";
        // 获取令牌并以您自己的方式返回它
        return Task.FromResult(token);
    }

    public AllowedHostsValidator AllowedHostsValidator { get; }
}

然后按照以下方式创建GraphServiceClient:

var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new TokenProvider());
var graphServiceClient = new GraphServiceClient(authenticationProvider);

从流畅API中删除Request()

在SDK的早期版本中,调用涉及在请求API中调用Request(),如下所示:

var user = await graphServiceClient
.Me
.Request()  // 这已被移除
.GetAsync();

在V5中,类似的调用将删除Request()部分,如下所示:

var user = await graphServiceClient
.Me
.GetAsync();
英文:

The v5 SDK had a lot of changes, you can read about it here :

https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md

Authentication

In place of the DelegateAuthenticationProvider, custom authentication flows can be done creating an implementation of IAccessTokenProvider, and using with the BaseBearerTokenAuthenticationProvider from the Kiota abstractions as follows

public class TokenProvider : IAccessTokenProvider
{
    public Task&lt;string&gt; GetAuthorizationTokenAsync(Uri uri, Dictionary&lt;string, object&gt; additionalAuthenticationContext = default,
        CancellationToken cancellationToken = default)
    {
        var token = &quot;token&quot;;
        // get the token and return it in your own way
        return Task.FromResult(token);
    }

    public AllowedHostsValidator AllowedHostsValidator { get; }
}

Then create the GraphServiceClient as follows

var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new TokenProvider());
var graphServiceClient = new GraphServiceClient(authenticationProvider);

Removal of Request() from the fluent API

In previous versions, of the SDK, calls involved the calling of Request() in the request API as follows

var user = await graphServiceClient
.Me
.Request()  // this is removed
.GetAsync();

A similar call in the V5 will have the Request() section removed to be called as below.

var user = await graphServiceClient
.Me
.GetAsync();

huangapple
  • 本文由 发表于 2023年6月9日 14:32:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定