As the planet is under central force (gravitational interaction), its angular momentum is conserved about the Sun (which is situated at one of the focii of the ellipse)
So, mv1r1=mv2r2 or, v21=v22r22r21 (1)
From the conservation of mechanical energy of the system (Sun+planet),
−γmsmr1+12mv21=−γmsmr2+12mv22
or, −γmsr1+12v22r22r21=−(γmsr2)+12v22 [Using (1)]
Thus, v2=√2γmsr1r2(r1+r2) (2)
Hence M=mv2r2=m√2γmsr1r2(r1+r2)