要通过微软图形应用程序接口(Microsoft Graph API)访问当前SharePoint Online用户的“喜欢”和“保存以供以后使用”文档/页面,您需要了解以下几个基础概念:
首先,您需要在Azure AD中注册您的应用程序,并获取客户端ID和密钥。然后,配置所需的权限,例如Sites.Read.All
和User.Read
。
使用OAuth 2.0流程获取访问令牌。以下是一个简单的示例代码,展示如何使用Python和requests
库来获取访问令牌:
import requests
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
tenant_id = 'YOUR_TENANT_ID'
token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'
data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'https://graph.microsoft.com/.default'
}
response = requests.post(token_url, data=data)
access_token = response.json().get('access_token')
使用获取到的访问令牌,您可以调用Graph API来获取用户的“喜欢”和“保存以供以后使用”的文档。以下是一个示例:
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# 获取用户喜欢的文档
likes_url = 'https://graph.microsoft.com/v1.0/me/insights/liked'
likes_response = requests.get(likes_url, headers=headers)
likes_data = likes_response.json()
# 获取用户保存以供以后使用的文档
saves_url = 'https://graph.microsoft.com/v1.0/me/insights/saved'
saves_response = requests.get(saves_url, headers=headers)
saves_data = saves_response.json()
通过以上步骤,您应该能够成功地通过Microsoft Graph API访问当前SharePoint Online用户的“喜欢”和“保存以供以后使用”的文档/页面。