我正在尝试使用应用程序注册从 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
向特定站点授予权限有多种不同的方法。