首页 > 解决方案 > 尝试从共享点获取工作簿时出现 Microsoft Graph ItemNotFound 错误

问题描述

我遵循了本教程:https ://mihai-albert.com/2020/05/13/using-microsoft-graph-to-modify-excel-files-stored-in-sharepoint-online-with-c/

当我使用图形资源管理器并执行 GET https://graph.microsoft.com/v1.0/drives/{drive-id}/root/search(q='{search-text}')?select=name ,id,webUrl 然后我可以毫无问题地获得项目 ID。但是当我想在我的应用程序中获取工作簿时

 private readonly GraphServiceClient _graphServiceClient;
        public GraphService(
           IOptions<AzureAdOptions> azureAdOptions)
        {
            var confidentialClientApplication = ConfidentialClientApplicationBuilder
                   .Create(azureAdOptions.Value.ClientId)
                   .WithTenantId(azureAdOptions.Value.TenantId)
                   .WithClientSecret(azureAdOptions.Value.ClientSecret)
                   .Build();

            var authProvider = new ClientCredentialProvider(confidentialClientApplication);
            _graphServiceClient = new GraphServiceClient(authProvider);
        }

        public async Task Test()
        {
            try {
                var range = await _graphServiceClient.Drives[{drive-id}].Items[{item-id}]
                .Request()
                .GetAsync();
               

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

然后我得到一个 itemnotfound 错误,尽管输入了正确的 id。如果我只是添加该部分,它可以找到驱动器,因此我的设置似乎没有问题。有没有人遇到过这个问题或知道如何解决这个问题?

标签: c#microsoft-graph-api

解决方案


使用应用程序权限调用GET /drives/{drive_id}/items/{item_id}时,您的应用程序需要此处列出的应用程序权限之一,很可能是Files.Read.All

我观察到,当您拥有一个不具有所需权限之一的 access_token 时,您会得到具有误导性的itemNotFound

在您的情况下,只需将Files.Read.All应用程序权限(需要管理员同意)添加到 Azure AD 上的应用程序。


推荐阅读