{"record":{"id":"c322c04dce306fcb","repo":"Foundry376/Mailspring","slug":"o365-profile-request-returned-meresp-status-m","errorCode":null,"errorMessage":"O365 profile request returned ${meResp.status} ${meResp.statusText}: ${JSON.stringify(me)}","messagePattern":"O365 profile request returned (.+?) (.+?): (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/internal_packages/onboarding/lib/onboarding-helpers.ts","lineNumber":294,"sourceCode":"  const { access_token, refresh_token, id_token } = await fetchPostWithFormBody<TokenResponse>(\n    `https://login.microsoftonline.com/common/oauth2/v2.0/token`,\n    {\n      code: code,\n      scope: O365_SCOPES.filter((f) => !f.startsWith('https://outlook.office.com')).join(' '),\n      client_id: O365_CLIENT_ID,\n      code_verifier: CODE_VERIFIER,\n      grant_type: `authorization_code`,\n      redirect_uri: `http://localhost:${LOCAL_SERVER_PORT}/desktop`,\n    }\n  );\n\n  // get the user's email address\n  const meResp = await fetch('https://graph.microsoft.com/v1.0/me', {\n    headers: { Authorization: `Bearer ${access_token}` },\n  });\n  const me = await meResp.json();\n  if (!meResp.ok) {\n    throw new Error(\n      `O365 profile request returned ${meResp.status} ${meResp.statusText}: ${JSON.stringify(me)}`\n    );\n  }\n  // The Graph API can return 200 OK with an error body in some edge cases\n  if (me.error) {\n    throw new Error(`O365 profile request failed: ${me.error.code}: ${me.error.message}`);\n  }\n\n  // Try multiple sources to find the email address. For most work accounts `mail` or\n  // `userPrincipalName` is set. For personal MSA accounts or accounts without Exchange\n  // Online licenses, fall back to the id_token claims (requires openid+email scopes).\n  let emailAddress: string | null = me.mail || me.userPrincipalName || null;\n\n  if (!emailAddress && id_token) {\n    try {\n      // Decode id_token JWT payload (base64url encoded) to extract email claims\n      const payload = JSON.parse(Buffer.from(id_token.split('.')[1], 'base64').toString('utf8'));\n      const candidate: string = payload.email || payload.preferred_username || payload.unique_name;","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/Foundry376/Mailspring/blob/648c685d602ece6bb00c22534b8734de6ac644b3/app/internal_packages/onboarding/lib/onboarding-helpers.ts#L276-L312","documentation":"Guard in buildMicrosoftAccountFromAuthResponse: the Microsoft Graph /v1.0/me call made with the freshly obtained access token returned a non-ok status. The token exchange succeeded but the user profile could not be read, so O365/Outlook account construction fails; typically a missing User.Read scope or an invalid token audience.","triggerScenarios":"Thrown at app/internal_packages/onboarding/lib/onboarding-helpers.ts:294 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Confirm the O365_SCOPES used during authorization include Graph's User.Read","Retry onboarding to obtain a fresh token","Check the embedded JSON body for the Graph error code (e.g. InvalidAuthenticationToken)"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"648c685d602ece6bb00c22534b8734de6ac644b3","analyzedAt":"2026-09-03T02:00:24.311Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T07:17:11.731Z"}