如何使用应用程序注册客户端 ID 和密钥以编程方式检索共享点列表项?

问题描述 投票:0回答:1

我正在尝试使用应用程序注册从 api 调用中获取共享点列表项进行身份验证。 我的应用程序注册已经具有在应用程序级别(而不是委托)选择的权限sites.selected。 我能够生成令牌,但是当我尝试检索项目时,我收到了禁止。 我在这里可能缺少什么?

def __init__(self, tenantId, clientId, clientSecret):
    self.tenant_id = tenantId
    self.client_id = clientId
    self.client_secret = clientSecret
    self.resource = "https://graph.microsoft.com/.default"  # Microsoft Graph API endpoint
    url = f"https://login.microsoftonline.com/{self.tenant_id}/oauth2/v2.0/token"
    payload = {
        'grant_type': 'client_credentials',
        'client_id': self.client_id,
        'client_secret': self.client_secret,
        'scope': self.resource
    }
    headers = {"Content-Type": "application/x-www-form-urlencoded"}
    response = requests.post(url, data=payload, headers=headers)
    json_data = json.loads(response.text)
    self.token = json_data['access_token']


def get_sharepoint_list_content(self, site, listname, **kwargs):
    filter_query = kwargs.get('filter_query', '')
    columns = kwargs.get('columns', 'id')
    top = kwargs.get('top', '')

    url = f"{site}/_api/web/lists/GetByTitle('{listname}')/items?$select={columns}&$filter={filter_query}&$top={top}"
    headers = {
        'Accept': 'application/json;odata=nometadata',
        'Authorization': f'Bearer {self.token}'
    }

    list_items = []
    while url:
        response = requests.get(url, headers=headers)
        lista = json.loads(response.text)
        list_items.extend(lista.get('value', []))
        url = lista.get('@odata.nextLink', '')

    return list_items
azure sharepoint azure-active-directory azure-managed-identity
1个回答
0
投票

向特定站点授予权限有多种不同的方法。

  1. https://ashiqf.com/2021/03/15/how-to-use-microsoft-graph-sharepoint-sites-selected-application-permission-in-a-azure-ad-application-for-more-粒度控制/ - 使用 2 个不同的应用程序,其中一个具有完全控制权,授予对具有 site.selected 的应用程序的访问权限 - 很有用,因为它可以避免处理 SPO 权限。
  2. https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.sites/new-mgsitepermission?view=graph-powershell-1.0 - 这里的问题是执行此操作的身份需要足够的访问该网站。网站集管理员成员资格有效,使用 Set-SPOUser 授予,请注意,这些 SPO 管理命令仅适用于 Windows PowerShell。
  3. https://pnp.github.io/powershell/cmdlets/Grant-PnPAzureADAppSitePermission.html - 如果由于某种原因不需要 Graph API PowerShell,这是另一个选择。
© www.soinside.com 2019 - 2024. All rights reserved.