Outlook API是微软提供的一组RESTful接口,允许开发者访问Outlook.com、Office 365和Exchange Online中的邮件、日历和联系人数据。通过Graph API(Microsoft Graph的一部分),可以查询组织中的用户信息,包括他们的经理关系。
首先需要:
主要使用两个API端点:
https://graph.microsoft.com/v1.0/users/{user-id}
https://graph.microsoft.com/v1.0/users/{user-id}/manager
import requests
# 配置信息
tenant_id = "your-tenant-id"
client_id = "your-client-id"
client_secret = "your-client-secret"
user_email = "target-user@yourdomain.com" # 要查询的用户
# 获取访问令牌
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
token_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'https://graph.microsoft.com/.default'
}
token_response = requests.post(token_url, data=token_data)
access_token = token_response.json().get('access_token')
# 获取用户ID
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
user_url = f"https://graph.microsoft.com/v1.0/users/{user_email}"
user_response = requests.get(user_url, headers=headers)
user_id = user_response.json().get('id')
# 获取经理信息
manager_url = f"https://graph.microsoft.com/v1.0/users/{user_id}/manager"
manager_response = requests.get(manager_url, headers=headers)
manager_data = manager_response.json()
print(f"经理信息: {manager_data}")
const axios = require('axios');
const tenantId = 'your-tenant-id';
const clientId = 'your-client-id';
const clientSecret = 'your-client-secret';
const userEmail = 'target-user@yourdomain.com';
async function getManager() {
try {
// 获取访问令牌
const tokenResponse = await axios.post(
`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
new URLSearchParams({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope: 'https://graph.microsoft.com/.default'
})
);
const accessToken = tokenResponse.data.access_token;
// 获取用户ID
const userResponse = await axios.get(
`https://graph.microsoft.com/v1.0/users/${userEmail}`,
{
headers: {
'Authorization': `Bearer ${accessToken}`
}
}
);
const userId = userResponse.data.id;
// 获取经理信息
const managerResponse = await axios.get(
`https://graph.microsoft.com/v1.0/users/${userId}/manager`,
{
headers: {
'Authorization': `Bearer ${accessToken}`
}
}
);
console.log('经理信息:', managerResponse.data);
} catch (error) {
console.error('发生错误:', error.response?.data || error.message);
}
}
getManager();
需要以下权限之一:
通过以上方法,您可以有效地查询组织中任何联系人的经理信息,并集成到您的应用程序中。
没有搜到相关的文章