英文:
Connect to OneDrive using Microsoft Graph and C# not working
问题
我试图创建一个非常简单的应用程序,用于连接到我的OneDrive并获取我在OneDrive上托管的各种文件的列表。
这是代码:
string clientId = "我的客户端ID";
string clientSecret = "秘密ID值";
string tenantId = "我的租户ID";
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential);
var driveItems = await graphClient.Me.Drive.Root.Children.Request().GetAsync();
// 显示列表中所有项目的名称
foreach (var item in driveItems)
{
Console.WriteLine(item.Name);
}
问题是,显然 graphClient.Me.Drive 不包含 Root 定义(至少Visual Studio告诉我如此)。
我正在使用 Microsoft.Graph v5.11 和 Azure.Identity v1.9。
英文:
I was trying to create a very simple app to connect to my OneDrive and receive a list of the various files that I have hosted on OneDrive.
This is the code:
string clientId = "My client Id";
string clientSecret = "The secret id value";
string tenantId = "My tennant id";
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential);
var driveItems = await graphClient.Me.Drive.Root.Children.Request().GetAsync();
// Display the names of all items in the list
foreach (var item in driveItems)
{
Console.WriteLine(item.Name);
}
The problem is that apparently graphClient.Me.Drive does not contain a Root definition (at least that's what Visual Studio tells me).
I am using Microsoft.Graph v5.11 and Azure.Identity v1.9
答案1
得分: 1
你现在正在使用 Microsoft.Graph v5.11,与 V4.x 不同,在 V5 中没有 .Request()。
同时,你正在使用客户端凭据流 new ClientSecretCredential,因此不能使用 .Me,而必须使用 .Users["your_user_id"]。因为你没有首先登录,所以必须手动提供用户 id。顺便说一下,由于你正在使用客户端凭据流,你必须将范围设置为 var scopes = new[] { "https://graph.microsoft.com/.default" };。这要求你同意 API 权限的应用程序类型:
然后,基于 V5.11 的代码应该如下:
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenantId ";
var clientId = "clientId ";
var clientSecret = "clientSecret";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var userDriveId = await graphClient.Users["yourUserId"].Drive.GetAsync();
var driveItems = await graphClient.Drives["userDriveId"].Items["Root"].Children.GetAsync();
相关链接:https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/feature/5.0/docs/upgrade-to-v5.md#drive-item-paths
英文:
You are now using Microsoft.Graph v5.11 which is different from V4.x and in V5 there's no .Request().
In the meantime, you are using client credential flow new ClientSecretCredential, so you can't use .Me but have to use .Users["your_user_id"]. Because you don't sign in first, so you have to give a user id manually. By the way, since you are using client credential flow, you have to set the scope as var scopes = new[] { "https://graph.microsoft.com/.default" };. This requires you to consent application type of api permission:
Then you code based on V5.11 should be like:
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenantId ";
var clientId = "clientId ";
var clientSecret = "clientSecret";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var userDriveId = await graphClient.Users["yourUserId"].Drive.GetAsync();
var driveItems = await graphClient.Drives["userDriveId"].Items["Root"].Children.GetAsync();
Related link: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/feature/5.0/docs/upgrade-to-v5.md#drive-item-paths
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。





评论