Dark matter explanations of the gamma-ray excesses from the Galactic Center and M31