Sharepoint Online 列表项查询 returns 空数组与 Microsoft Graph API

Sharepoint Online list items query returns empty array with Microsoft Graph API

TLDR:我正在尝试使用 Powershell 中的 Microsoft Graph API 获取 Sharepoint Online 列表的列表项。但我得到的只是一个空数组。

我在 Azure 中创建了一个应用程序并赋予了它以下权限:

我知道我需要的权限太多了,但是为了进行故障排除并确保这不是权限问题,我将它们全部添加了。

这是我用来获取 Graph API 令牌的代码:

$TenantId = 'my-tenant-id'
$ClientId = 'my-client-id'
$ClientSecret = 'my-client-secret'

$Body = @{
    'tenant' = $TenantId
    'client_id' = $ClientId
    'scope' = 'https://graph.microsoft.com/.default'
    'client_secret' = $ClientSecret
    'grant_type' = 'client_credentials'
}

$Params = @{
    'Uri' = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
    'Method' = 'Post'
    'Body' = $Body
    'ContentType' = 'application/x-www-form-urlencoded'
}

$AuthResponse = Invoke-RestMethod @Params

$Headers = @{
    'Authorization' = "Bearer $($AuthResponse.access_token)"
}

当我运行以下命令时,只是为了获取列表信息,它起作用了。

$SiteID = "my-site-id"
$ListID = "my-list-id"

$url = "https://graph.microsoft.com/v1.0/sites/$($SiteID)/lists/$($ListID)"

$Result = Invoke-RestMethod -Uri $url -Headers $Headers -Method Get -ContentType "application/json"

$Result | fl

这给了我以下结果:

@odata.context       : https://graph.microsoft.com/v1.0/$metadata#sites('abc.sharepoint.com%2Csite-id')/lists/$entity
@odata.etag          : "list-id,1139"
createdDateTime      : 2020-09-22T08:18:49Z
description          :
eTag                 : "list-id,1139"
id                   : list-id
lastModifiedDateTime : 2020-10-21T14:53:18Z
name                 : list-name
webUrl               : https://abc.sharepoint.com/sites/my-site/Lists/my-list
displayName          : my-list
createdBy            : @{user=}
lastModifiedBy       : @{user=}
parentReference      : @{siteId=abc.sharepoint.com,list-id
                       cca2b90}
list                 : @{contentTypesEnabled=True; hidden=False; template=genericList}

所以我知道我使用 Graph API 的身份验证有效。

但是当我想要获取实际的列表项时,我得到了一个空数组。

命令:

$SiteID = "my-site-id"
$ListID = "my-list-id"

$url = "https://graph.microsoft.com/v1.0/sites/$($SiteID)/lists/$($ListID)/items?expand=fields"

$Result = Invoke-RestMethod -Uri $url -Headers $Headers -Method Get -ContentType "application/json"

$Result | fl

结果:

@odata.context : https://graph.microsoft.com/v1.0/$metadata#sites('abc.sharepoint.com%2Csite-id')/lists('list-id')/items
value          : {}

我不知道问题出在哪里。这里的类似帖子让我认为这是一个权限问题。但是我已经将书中的所有权限都扔给了它,它仍然没有返回列表项。

不胜感激。

由于您使用的是 Microsoft Graph API,因此您需要 select Microsoft Graph api Azure AD 门户中的权限,而不是共享点下的权限。 sharepoint下的权限与sharepoint相关 API.