0

I've got an app that synchs with Microsoft Outlook accounts. It was working quite well as early as last night, but have lately run in to a problem: every time I make an API call I get this error:

{"error"=>{"code"=>"MailboxInfoStale", "message"=>"Mailbox info is stale."}}

I know for a fact that the mailbox that I'm testing with isn't stale, as it was visited and used less than an hour ago. Here's my code:

# Get the emails between the user and the prospect
# We need to be aware of the user's MS email address, which is possible different than the one we have
# for them.

user_email = user_email || get_user_email(token, context)

if token
  conn = Faraday.new(:url => "https://outlook.office.com") do |faraday|
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
  end

  response = conn.get do |request|
    request.url "/api/v2.0/Me/Messages?$search=%22from:#{prospect_email}%22&$top=20"
    request.headers['Authorization'] = "Bearer #{token['token']}"
    request.headers['Accept'] = 'application/json'
    request.headers['X-AnchorMailbox'] = user_email
  end

  # Okay, this is great: MS tells us to JSON parse what they return, but whether or not they return valid JSON depends on the state of the
  # data that you request, so we'll force it by wrapping it in '[]'.
  parsed_response = JSON.parse("[#{response.body}]")

  if parsed_response[0]["value"].blank?
    # Returns an empty array because we're combining this method and #get_emails in API::ActivitiesController
    return []
  else
    messages = parsed_response[0]["value"]
  end
end

Why would MS return MailboxInfoStale?

octopushugs
  • 147
  • 1
  • 4
  • 13

3 Answers3

1

Changing the X-AnchorMailbox header worked for me.

Geek
  • 11
  • 2
0

I had similar problems and removed the X-AnchorMailbox header (which was filled with a wrong value for me) to fix it.

Chris
  • 3
  • 4
0

X-AnchorMailbox has to be set to the e-mail address connected to your credentials for example OAuth access token. It they differ you'll get this error. Of course it can be fixed by removing the header, but it may result in accessing wrong mailbox by mistake (if you have more than one for instance).

swojtasiak
  • 606
  • 6
  • 12