Python微软信箱OAuth2认证Demo

  • 相较于SMTP, oauth2效率更高更安全
  • oauth2认证至少需要email, clientid, refresh_token
  • 下面的demo通过email,clientid,refresh_token获取access_token, 然后使用access_token读取邮件列表, 更多功能自行开发

示例代码:

# -*- coding: utf-8 -*-
import email as email_reader
import random
import ssl
from email.header import decode_header
from enum import Enum
import requests
import imaplib

def get_access_token_from_refresh_token(refresh_token, client_id):
headers = {
‘Host’: ‘login.microsoftonline.com’,
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36’,
‘Content-Type’: ‘application/x-www-form-urlencoded;charset=UTF-8’,
}
data = {
“client_id”: client_id,
“refresh_token”: refresh_token,
“grant_type”: “refresh_token”
}
rr = requests.post(“https://login.microsoftonline.com/common/oauth2/v2.0/token”, headers=headers, data=data)

if rr.json().get(“error”) is None:
return {“code”: 0, “access_token”: rr.json()[“access_token”], “refresh_token”: rr.json()[“refresh_token”]}
if rr.json().get(“error_description”).find(“User account is found to be in service abuse mode”) != -1:
return {“code”: 1, “message”: “account was blocked or wrong username,password,refresh_token,client_id”}
return {“code”: 1, “message”: “get access token is wrong”}

def imap_authenticate_with_oauth2(username, access_token):
auth_string = f”user={username}\1auth=Bearer {access_token}\1\1″
mail = imaplib.IMAP4_SSL(“outlook.office365.com”)
mail.authenticate(“XOAUTH2”, lambda x: auth_string)
return mail
def read_mail(email, access_token):
mail = imap_authenticate_with_oauth2(email, access_token)

mail.list()
mail.select(“inbox”)
status, messages = mail.search(None, ‘ALL’)

messages = messages[0].split()
# print(status)
for mail_id in messages:
status, msg_data = mail.fetch(mail_id, ‘(RFC822)’)
email_msg = msg_data[0][1].decode(‘utf-8’)
print(email_msg)
print(“done”)

#oauth2认证至少需要email, clientid, refresh_token
#下面的demo通过email,clientid,refresh_token获取access_token, 然后使用access_token读取邮件列表, 更多功能自行开发
#卡密(格式:email—-密码—-clientid—-refresh_token—-access_token)
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V8YTqAaA/IDYpYtWF5B2+ZLwYrWqvqZFuSBvi7852TgUMxr2z7UA5qDCwN40q1r+6tolH7Jde9P2858Bn3lTAybNtZ+4cqiu5GXnAC4QDDQ8lHiCaNLFGalzlRcGA4PYzaRtzG/88cFhurvBqCCscgom52T5PCUttoMpH/b3hJ9QB7YBMkvvJNMRAUZVswAiEt5GchIDaldAYuGLF1NbfeEtNiDmvkEvLx//aIlySwJFB43B/VuqzY4D2aFSWjXoqg0LD5BGICJb345y642+as+1i/uwtwU6hR5tTgkxVA999qFAUR2PcREdEjGaWWqSdm33WTCcY+8yiKfCUNJ6jRa+lzMTyZxnuto6TuLSgKDhG9hkhfx1hoqyljpc/rr275XmYQMxlDsBDSQSFzjhrtf/5SBoeKMRPenKs0Ptkbv5qin7k/uS/P2zoMAcHdg69qZS6zwWIuV1xwEWh8OjymSZJMDKZ+yea09rWWE5kGQvLTs4RwJ/JJXXoOYFjHK3wIZjhN29druDTnFlCBLn/iMy0gt7XKgemX5i8kZNXBDXuV5xrA+8QEcC
def example():
email=”[email protected]
client_id=”8b4ba9dd-3ea5-4e5f-86f1-ddba2230dcf2″
refresh_token=”M.C525_BAY.0.U.-CvxMhLe1rlmKZ3H6fEdCrNRAggUCP5Z55X1C3lwChG0YP7CbpvsWBNTR778D8VEcZ2YPv8iUGUCWxx*f0j4SIjSz3HX!4pFIvFD1OR6udxZEiv*6E!2sqwU4qoYk410ie31TxjhVaUoXbZ5xXCHYxvYzJjPEZ1PeibIxBau4N!Duie73iVM!vGY81tJuFLPTFahzdB6Eu9CftyxqloI7TO*elPnuIIr0NJ*4g1vcQV0qiJuUUOKGDBFIaRUaUtbahmS66O*ZqOub5HoiZ*zZLHeZ!vAL9usCz3TOsMI82KxCK50dgSv87dbn1tryQ!VrKWpZcoPlP6YgjWFelADWSJMBAvnUOTWkBjNclHO35WnqeMh!OdtWqmP1HktZWCPEytOBU8RynGl7Z2j7ct!Q7tqUoOmiczZ!y1LGK3deUFPWA9IHdtJYxl2yEGBYkbQbDA$”
token=get_access_token_from_refresh_token(refresh_token, client_id)
read_mail(email, token[“access_token”])

#调用示例
example()

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。