Files
outlookapi/example.py
huangzhenpc 4d8ea56a45 xx
2025-03-26 11:39:59 +08:00

109 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import json
# API接口地址假设API服务运行在本地5000端口
API_URL = "http://localhost:5000"
client_id = '9e5f94bc-e8a4-4e73-b8be-63364c29d753'
email = 'eedbbfdd186@outlook.com'
refresh_token ='M.C544_BL2.0.U.-CmrNXHVufVZdj1*CaDuSw4WSQYVfF7ILMi4XYHeVQ!YJm56uJO5HPG9I2bOJIRrS3c5FgP9sDKB*HjA3O6wVY4Cr7hzNGWjujT*xZ5k4gOjRDVXx9ocaY1bf5J2HZgAoBJYjFq76*3h2xddMEGqp7iFjYDo3B9rcfRGh!G6rJ38vkWBSGw!7hcj21IWdZD!eIZqCx1o2tDrzeH*fRnuf*DoTQEFCDnFpCoulmQHDUBEFiBT8H*TzupejEgWTmXewN9tpcQwFituIbGScsDWdRuB5pcF63p7jazZdeZ8Bpa7pQb5Fc4mYUSwQS4Qx9CNNMYnwYuhiAVEXPcoppWCA7WXF!bgOxa7IuZASnWMiC!jqUu77KnwrHWZD14SDrFfwBQ$$'
def get_emails():
"""获取最新的邮件"""
url = f"{API_URL}/api/emails"
payload = {
"email": email,
"client_id": client_id,
"refresh_token": refresh_token,
"folder": "INBOX", # 可以改为其他文件夹
"limit": 10 # 获取最新的10封邮件
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status() # 如果请求失败,抛出异常
result = response.json()
# 保存新的refresh_token(如果有)
if "refresh_token" in result and result["refresh_token"] != refresh_token:
print(f"新的refresh_token: {result['refresh_token']}")
# 在实际应用中你应该将新的refresh_token保存到安全的地方
# 显示邮件
print(f"找到 {len(result['emails'])} 封邮件:")
for i, email_data in enumerate(result["emails"], 1):
print(f"\n--- 邮件 {i} ---")
print(f"主题: {email_data.get('subject', 'N/A')}")
print(f"发件人: {email_data.get('from', 'N/A')}")
print(f"日期: {email_data.get('date', 'N/A')}")
# 如果邮件有附件
if "attachments" in email_data:
print(f"附件: {', '.join(email_data['attachments'])}")
# 显示邮件正文截取前100个字符
if "body" in email_data:
body_preview = email_data["body"][:100] + "..." if len(email_data["body"]) > 100 else email_data["body"]
print(f"内容预览: {body_preview}")
return result
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
return None
def get_folders():
"""获取邮箱文件夹列表"""
url = f"{API_URL}/api/folders"
payload = {
"email": email,
"client_id": client_id,
"refresh_token": refresh_token
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
# 保存新的refresh_token(如果有)
if "refresh_token" in result and result["refresh_token"] != refresh_token:
print(f"新的refresh_token: {result['refresh_token']}")
# 显示文件夹
print("邮箱文件夹列表:")
for folder in result["folders"]:
print(f"- {folder}")
return result
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
return None
if __name__ == "__main__":
print("1. 获取邮箱文件夹列表")
print("2. 获取最新邮件")
choice = input("请选择操作 (1/2): ")
if choice == "1":
get_folders()
elif choice == "2":
get_emails()
else:
print("无效的选择")