"The tenant for tenant guid does not exist" 使用图谱 API 时 - 用户类型为成员的事件

"The tenant for tenant guid does not exist" when using GraphAPI - Even with user type as Member

我正在尝试使用 Microsoft Graph API 访问电子邮件。当我尝试访问电子邮件时出现以下错误。

Microsoft.Graph.ServiceException: 'Code: OrganizationFromTenantGuidNotFound

Message: The tenant for tenant guid '<some id>' does not exist.

这是获取电子邮件的代码

var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
    return Task.CompletedTask;
}));

var userId = "quicksilverconnect@outlook.com"; // Tried vijaynirmal@quicksilverconnectoutlook.onmicrosoft.com also
var messageId = "Actual message id";

var email = await graphServiceClient.Users[userId].Messages[messageId].Request().GetAsync();

这是获取访问令牌的代码

private const string _clientId = "xxxxxxx-xxxxxx-xxxxxxx-xxxx";
private const string _clientSecret = "xxxxxxx-xxxxxx-xxxxxxx-xxxx";
private const string _tenantName = "ecd90453-34b6-xxxx-xxxx-xxxxxxxxx";
private readonly string _uri = $"https://login.microsoftonline.com/{_tenantName}/oauth2/v2.0/token";
private const string _grantType = "client_credentials";
private const string _scope = "https://graph.microsoft.com/.default";

public async Task<string> GetAccessTokenAsync()
{
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("Grant_Type", _grantType),
        new KeyValuePair<string, string>("Scope", _scope),
        new KeyValuePair<string, string>("Client_Id", _clientId),
        new KeyValuePair<string, string>("Client_Secret", _clientSecret)
    });
    var responce = await _httpClient.PostAsync(_uri, content);

    responce.EnsureSuccessStatusCode();

    var jsonString = await responce.Content.ReadAsStringAsync();

    var document = await JsonDocument.ParseAsync(jsonString.ToStream());

    return document.RootElement.GetProperty("access_token").GetString();
}

我在网上搜索了解决方案。我找到了一些解决方案,但其中 none 对我有用。

  1. 用户类型必须是会员。我的用户类型已经是会员。原始问题 -

  2. 使用域作为 tenentId。它不工作。原始问题 - Getting "The tenant for tenant guid '' does not exist"

     private const string _tenantName = "quicksilverconnectoutlook.onmicrosoft.com";
    

一些有趣的观察

更新:

我想使用应用程序权限而不是委派权限,因为我的应用程序将使用通知订阅在任何用户收到新邮件时获得通知。另外,我想获取新邮件的完整电子邮件详细信息。简而言之,此应用程序将 运行 在后台运行。

var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
    return Task.CompletedTask;
}));

var subscription = await graphServiceClient.Subscriptions.Request().AddAsync(new Subscription()
{
    Resource = "/users/quicksilverconnect@outlook.com/messages",
    ChangeType = "created",
    ExpirationDateTime = DateTimeOffset.Now.AddDays(3).AddHours(-1),
    NotificationUrl = "https://asdasdasd.azurewebsites.net/Outlook/NewMailListener",
    ClientState = Guid.NewGuid().ToString()
});

看来问题是因为您没有订阅 O365。虽然您有 azure 订阅并且有一个用于您的 azure 帐户的电子邮件,但您没有 O365 订阅。所以你只能通过图表获取用户,但不能通过图表获取电子邮件。

对于这个问题,你可以去这个page(用你的Azure管理员账户登录)然后购买O365订阅。(例如:Office 65 E3

也许您也可以在同一页面上购买 Exchange online(例如 Exchange Online (Plan 2))以访问电子邮件。

顺便说一句,你的代码有错误。您将 client_credentials 用作“Grant_Type”并使用 DelegateAuthenticationProvider。如果要使用DelegateAuthenticationProvider,则需要将“Grant_Type”设置为password而不是client_credentials.

要使用客户端凭证认证,您需要安装Microsoft.Graph.Auth。注意:这是一个预发布包。这是一个代码片段

var confidentialClientApplication = ConfidentialClientApplicationBuilder
                                            .Create(configuration.ClientId)
                                            .WithTenantId(configuration.TenantId)
                                            .WithClientSecret(configuration.ClientSecret)
                                            .Build();
var authProvider = new ClientCredentialProvider(confidentialClientApplication);
var graphServiceClient = new GraphServiceClient(_clientCredentialProviderauthProvider);