This guide explains how to obtain the necessary credentials to use OAuth2 with Gmail (including Advanced Protection) and Office 365.
Caution
Advanced Protection Program (APP) Restriction
If your personal Google account is enrolled in the Advanced Protection Program, Google will block any private OAuth2 project (like the one created in this guide) from accessing your mail. You will see a 400: policy_enforced error.
Personal accounts with APP enabled cannot use this add-on via OAuth2 or Password. The only known workarounds are:
- Use a Google Workspace account (where an admin can whitelist the Client ID).
- Disable Advanced Protection (not recommended).
- Use a different account that is not enrolled in APP.
| Field | Required for Password | Required for Google OAuth2 | Required for Microsoft OAuth2 |
|---|---|---|---|
| Username | ✅ | ✅ | ✅ |
| Password | ✅ | ❌ | ❌ |
| Client ID | ❌ | ✅ | ✅ |
| Client Secret | ❌ | ✅ | ✅ |
| Refresh Token | ❌ | ✅ | ✅ |
| Tenant ID | ❌ | ❌ |
Note
The Refresh Token is strictly required for both Google and Microsoft. It allows the add-on to keep syncing in the background after your initial login expires (usually after 1 hour).
- Go to the Google Cloud Console.
- Click Select a project -> New Project.
- Name it "Home Assistant Imapsync" and click Create.
- Go to APIs & Services -> OAuth consent screen.
- Select External and click Create.
- Fill in the "App information" (App name, User support email).
- Add your email to "Developer contact information".
- Click Save and Continue.
- Scopes: Click Add or Remove Scopes.
- Manually add:
https://mail.google.com/ - Click Save and Continue.
- Test Users: Add your own email address.
- Go to APIs & Services -> Credentials.
- Click Create Credentials -> OAuth client ID.
- Select Web application.
- Authorized redirect URIs: Add
https://developers.google.com/oauthplayground - Click Create and save your Client ID and Client Secret.
- Go to the OAuth 2.0 Playground.
- Click the gear icon (top right) and check Use your own OAuth credentials.
- Enter your Client ID and Client Secret.
- In Step 1, enter
https://mail.google.com/in the input field and click Authorize APIs. - Log in and allow permissions.
- In Step 2, click Exchange authorization code for tokens.
- Copy the Refresh token.
- Go to the Azure Portal -> Microsoft Entra ID (formerly Azure AD).
- Go to App registrations -> New registration.
- Name: "Home Assistant Imapsync".
- Supported account types: "Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts".
- Redirect URI: Select Web and enter
https://oauth.pstmn.io/v1/callback. - Click Register.
- Go to API permissions -> Add a permission.
- Select Microsoft Graph -> Delegated permissions.
- Search and add:
IMAP.AccessAsUser.All,offline_access. - Click Add permissions.
- Go to Certificates & secrets -> New client secret.
- Name it "Addon secret" and click Add.
- Copy the Value (this is your Client Secret).
The easiest way is using a tool like Postman or a simple script.
- Auth URL:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize - Access Token URL:
https://login.microsoftonline.com/common/oauth2/v2.0/token - Scope:
https://outlook.office.com/IMAP.AccessAsUser.All offline_access - Grant Type: Authorization Code.
Add the harvested values to your sync job:
jobs:
- source_host: imap.gmail.com
source_user: your-email@gmail.com
source_auth_type: oauth2
source_oauth2_client_id: 'your-client-id'
source_oauth2_client_secret: 'your-client-secret'
source_oauth2_refresh_token: 'your-refresh-token'