Binance Pay checkout · instant single-file delivery after confirmation
Live Proof Log In Sign Up
← All Technical Guides
3 min read · Updated 2026-12-01

Node.js Microsoft OAuth2 Token Refresh for Outlook IMAP & Graph API

Complete Node.js example for refreshing Microsoft OAuth2 tokens from email|password|refresh_token|client_id lines and building XOAUTH2 SASL strings.

1. Exchanging refresh_token in Node.js (Built-in fetch)

Node.js 18+ includes native fetch, so you can exchange any MailStock refresh_token for an access_token without extra dependencies.

2. Node.js Token Refresh & XOAUTH2 Helper

async function getMicrosoftAccessToken(clientId, refreshToken) {
  const body = new URLSearchParams({
    client_id: clientId,
    refresh_token: refreshToken,
    grant_type: 'refresh_token',
    scope: 'https://outlook.office.com/IMAP.AccessAsUser.All offline_access',
  });
  const res = await fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body,
  });
  if (!res.ok) throw new Error('Token refresh failed: ' + res.status);
  const data = await res.json();
  return data.access_token;
}

function buildXoauth2Base64(email, accessToken) {
  const sasl = `user=${email}\x01auth=Bearer ${accessToken}\x01\x01`;
  return Buffer.from(sasl, 'utf8').toString('base64');
}

3. Automating Bulk Purchases with MailStock REST API v1

Need your Node.js app or Telegram bot to buy accounts on demand? Use POST /api/v1/orders with your Bearer API key to purchase 1 to 10,000 accounts in a single JSON call.

Ready to test with aged 12–36mo OAuth2 accounts? Instant .txt delivery via Binance Pay or Wallet · 24h DOA replacement guarantee.
Buy Aged Outlook OAuth2 Accounts → API Docs