Suppose we had two black bodies at distinct heights in an uniform gravitational field. They are connected by an insulating tube that only allows the exchange of electromagnetic radiation. Since radiation from the body at the bottom gets redshifted when it reaches the top, it loses energy. Similarly, radiation from the top body gets blueshifted when it reaches the bottom. So, if the bodies were initially at equal temperatures, they exchange a net energy and one heats up while the other one cools down.
If we now bring the bodies together we can use the temperature difference to do some work, then restore the system and repeat this indefinitely.
Since it appears that we broke the Second Law of Thermodynamics, there must be something wrong with this reasoning. What am I missing?