使用Python读取共享点列表项

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

我想使用图形 API 读取共享点列表项。我有这段代码,它允许我使用访问令牌获取 SharePoint 列表的列表。

from pypac import PACSession
session = PACSession()
headers = {
    'authorization': f'Bearer {access_token}',
}

response = session.request("GET",
    'https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com:/sites/xxx/xxx/BTADMCA:/lists?select=id,name',
    headers=headers,
)
response.json()

我有两个问题,我只获取列表 id,但无法检索列表中的行,第二个问题是我不知道如何从图形 api 获取新的访问令牌。我的帐户启用了 MFA,但没有共享点的客户端密钥,因为我没有管理员权限。我必须使用手机中的代码登录并批准登录才能登录

您可以帮助获取图形访问令牌并从列表中读取行吗?

Sharepoint 链接 https://xxxx.sharepoint.com/sites/xxx/xxxx/BTADMCA/Lists/Central/AllItems.aspx

我尝试了以下代码来获取访问代码:

import adal

authority_url = "https://login.microsoftonline.com/common"
client_id = "de8bc8b5-d9f9-48b1-a8ad-b748da725064"

auth_context = adal.AuthenticationContext(authority_url)

# Initiate device code flow
device_code = auth_context.acquire_user_code("https://graph.microsoft.com/", client_id)
print(device_code['message'])

# Poll for token using device code
token_response = auth_context.acquire_token_with_device_code("https://graph.microsoft.com/", device_code, client_id)

access_token = token_response["accessToken"]
print("Access token acquired successfully:", access_token)

我收到以下错误:

AdalError                                 Traceback (most recent call last)
<ipython-input-24-afa3aa1fa6be> in <module>
     11 
     12 # Poll for token using device code
---> 13 token_response = auth_context.acquire_token_with_device_code("https://graph.microsoft.com/", device_code, client_id)
     14 
     15 access_token = token_response["accessToken"]

c:\ProgramData\python36\lib\site-packages\adal\authentication_context.py in acquire_token_with_device_code(self, resource, user_code_info, client_id)
    309             return token
    310 
--> 311         return self._acquire_token(token_func, user_code_info.get('correlation_id', None))
    312 
    313     def cancel_request_to_get_token_with_device_code(self, user_code_info):

c:\ProgramData\python36\lib\site-packages\adal\authentication_context.py in _acquire_token(self, token_func, correlation_id)
    126             correlation_id or self.correlation_id, self._call_context.get('enable_pii', False))
    127         self.authority.validate(self._call_context)
--> 128         return token_func(self)
    129 
    130     def acquire_token(self, resource, user_id, client_id):

c:\ProgramData\python36\lib\site-packages\adal\authentication_context.py in token_func(self)
    302                 self._token_requests_with_user_code[key] = token_request
...
--> 362                                 wire_response)
    363             else:
    364                 try:

AdalError: Unexpected polling state invalid_client
python sharepoint azure-active-directory microsoft-graph-api
1个回答
0
投票
import requests

# Step 1: Obtain Authorization Code interactively
authorization_code = "your_authorization_code_here"

# Step 2: Exchange Authorization Code for Access Token
token_url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
data = {
    'client_id': 'your_client_id_here',
    'scope': 'https://graph.microsoft.com/.default',
    'code': authorization_code,
    'redirect_uri': 'http://localhost',  # Must match the redirect URI configured in your Azure app
    'grant_type': 'authorization_code',
}

# Make a POST request to obtain the access token
token_response = requests.post(token_url, data=data)

# Extract the access token from the response
access_token = token_response.json().get('access_token')

# Step 3: Use Access Token to Retrieve List Items
# Replace {site-id} and {list-id} with your SharePoint site and list IDs
list_items_url = 'https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items'
headers = {
    'Authorization': f'Bearer {access_token}',
}

response = requests.get(list_items_url, headers=headers)
list_items = response.json()

# list_items contains the items from your SharePoint list
print(list_items)
© www.soinside.com 2019 - 2024. All rights reserved.