在向Azure AD中托管的已注册图形API进行身份验证后,我在访问SharePoint站点文档库甚至用户OneDrive时遇到问题.好像有什么东西不见了?我使用的是最新版本的微软图形(5.6).
此代码可以工作:
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Drives.Item.List.Items.Item.DriveItem;
namespace GraphApp
{
class GraphHelper
{
//Settings object
private static Settings? _settings;
// User auth token credential
private static DeviceCodeCredential? _deviceCodeCredential;
// Client configured with user auth
private static GraphServiceClient? _userClient;
public static void InitializeGraphForUserAuth(Settings settings, Func<DeviceCodeInfo, CancellationToken, Task> deviceCodePrompt)
{
_settings = settings;
var options = new DeviceCodeCredentialOptions
{
ClientId = settings.ClientId,
TenantId = settings.TenantId,
DeviceCodeCallback = deviceCodePrompt,
};
_deviceCodeCredential = new DeviceCodeCredential(options);
_userClient = new GraphServiceClient(_deviceCodeCredential, settings.GraphUserScopes);
}
public static async Task<string> GetUserTokenAsync()
{
//Ensure cred isn't null
_ = _deviceCodeCredential ??
throw new System.NullReferenceException("Graph has not been initialized for auth");
//Ensure scopes aren't null
_ = _settings?.GraphUserScopes ?? throw new System.NullReferenceException("Arugment 'scopes' cannot be null");
// Request token with given scopes
var context = new TokenRequestContext(_settings.GraphUserScopes);
var response = await _deviceCodeCredential.GetTokenAsync(context);
return response.Token;
}
public static Task<User?> GetUserAsync()
{
//Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialzed for user auth");
return _userClient.Me.GetAsync((config) =>
{
//Only request specific properties
config.QueryParameters.Select = new[] { "displayName", "mail", "userPrincipalName" };
});
}
MS文档指出,我应该有一个.Me.Drive.Root,但绝对没有显示在这里:我用于获取SharePoint在线驱动器的代码.
不确定我做错了什么,我找不到任何文档
我已经try 了所有方法,除了将HTTP GET请求发送到GRAPH之外,如果可能的话,我想避免这样做
图形帮助器的完整代码:
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Drives.Item.List.Items.Item.DriveItem;
namespace GraphApp
{
class GraphHelper
{
//Settings object
private static Settings? _settings;
// User auth token credential
private static DeviceCodeCredential? _deviceCodeCredential;
// Client configured with user auth
private static GraphServiceClient? _userClient;
public static void InitializeGraphForUserAuth(Settings settings, Func<DeviceCodeInfo, CancellationToken, Task> deviceCodePrompt)
{
_settings = settings;
var options = new DeviceCodeCredentialOptions
{
ClientId = settings.ClientId,
TenantId = settings.TenantId,
DeviceCodeCallback = deviceCodePrompt,
};
_deviceCodeCredential = new DeviceCodeCredential(options);
_userClient = new GraphServiceClient(_deviceCodeCredential, settings.GraphUserScopes);
}
public static async Task<string> GetUserTokenAsync()
{
//Ensure cred isn't null
_ = _deviceCodeCredential ??
throw new System.NullReferenceException("Graph has not been initialized for auth");
//Ensure scopes aren't null
_ = _settings?.GraphUserScopes ?? throw new System.NullReferenceException("Arugment 'scopes' cannot be null");
// Request token with given scopes
var context = new TokenRequestContext(_settings.GraphUserScopes);
var response = await _deviceCodeCredential.GetTokenAsync(context);
return response.Token;
}
public static Task<User?> GetUserAsync()
{
//Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialzed for user auth");
return _userClient.Me.GetAsync((config) =>
{
//Only request specific properties
config.QueryParameters.Select = new[] { "displayName", "mail", "userPrincipalName" };
});
}
public static Task<MessageCollectionResponse?> GetInboxAsync()
{
//Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
return _userClient.Me
//Only messages from Inbox
.MailFolders["Inbox"]
.Messages
.GetAsync((config) =>
{
//Only request specific properties
config.QueryParameters.Select = new[] { "from", "isRead", "receivedDateTime", "subject" };
// Get only top 25 results
config.QueryParameters.Top = 25;
//Sort by received time, newest first
config.QueryParameters.Orderby = new[] { "receivedDateTime DESC" };
});
}
public static async Task SendMailAsync(string subject, string body, string recipient)
{
//ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
//Create the message
var message = new Message
{
Subject = subject,
Body = new ItemBody
{
Content = body,
ContentType = BodyType.Text
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = recipient
}
}
}
};
//Send the message
await _userClient.Me
.SendMail
.PostAsync(new SendMailPostRequestBody
{
Message = message
});
}
public static async Task GetSiteDetails(string sitename) //doesn't work
{
//ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var result = await _userClient.Sites.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Search = $"{sitename}";
});
}
public static async Task GetDrive()
{
//ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var childItems = _userClient.Me.Drive.
}
}
}