UNPKG

gmail-to-exchange365

Version:

Complete Gmail to Exchange 365 migration tool with UI - Migrate emails, attachments, and folders seamlessly

76 lines (64 loc) 2.24 kB
import axios from "axios"; const MS_AUTH_URL = "https://login.microsoftonline.com"; const MS_GRAPH_URL = "https://graph.microsoft.com"; export function getMSAuthUrl() { const tenantId = process.env.MS_TENANT_ID || "common"; const clientId = process.env.MS_CLIENT_ID!; const redirectUri = encodeURIComponent(process.env.MS_REDIRECT!); const scopes = [ "https://graph.microsoft.com/Mail.Read", "https://graph.microsoft.com/Mail.ReadWrite", "https://graph.microsoft.com/MailboxSettings.ReadWrite", "offline_access" ].join(" "); return `${MS_AUTH_URL}/${tenantId}/oauth2/v2.0/authorize?` + `client_id=${clientId}&` + `response_type=code&` + `redirect_uri=${redirectUri}&` + `response_mode=query&` + `scope=${encodeURIComponent(scopes)}`; } export async function exchangeMSCode(code: string) { const tenantId = process.env.MS_TENANT_ID || "common"; const clientId = process.env.MS_CLIENT_ID!; const clientSecret = process.env.MS_CLIENT_SECRET!; const redirectUri = process.env.MS_REDIRECT!; const response = await axios.post( `${MS_AUTH_URL}/${tenantId}/oauth2/v2.0/token`, new URLSearchParams({ client_id: clientId, client_secret: clientSecret, code: code, redirect_uri: redirectUri, grant_type: "authorization_code", scope: "https://graph.microsoft.com/.default" }), { headers: { "Content-Type": "application/x-www-form-urlencoded" } } ); return response.data; } export async function refreshMSToken(refreshToken: string) { const tenantId = process.env.MS_TENANT_ID || "common"; const clientId = process.env.MS_CLIENT_ID!; const clientSecret = process.env.MS_CLIENT_SECRET!; const response = await axios.post( `${MS_AUTH_URL}/${tenantId}/oauth2/v2.0/token`, new URLSearchParams({ client_id: clientId, client_secret: clientSecret, refresh_token: refreshToken, grant_type: "refresh_token", scope: "https://graph.microsoft.com/.default" }), { headers: { "Content-Type": "application/x-www-form-urlencoded" } } ); return response.data; }