To use the Outlook REST API to retrieve emails from a user’s mailbox:
1. Authentication : Authenticate the user and obtain an access token using OAuth 2.0.
2. Making API Requests : Use the access token to make HTTP GET requests to the Outlook REST API endpoint, such as
https://graph.microsoft.com/v1.0/me/messages
.
3. Handling Responses : Parse the JSON response to extract the information you need.
Example in Python :
import requests
def get_emails(access_token):
url = "https://graph.microsoft.com/v1.0/me/messages"
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/json"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
emails = response.json()
return emails['value']
else:
raise Exception(f"Error: {response.status_code}")
# Example usage
access_token = "YOUR_ACCESS_TOKEN"
emails = get_emails(access_token)
for email in emails:
print(email['subject'])