PHB, page 195:
You can make an opportunity attack when a hostile creature you can see moves out of your reach.
So regardless of whether you're holding a weapon or not, you can make an opportunity attack whenever an enemy moves out of your reach.
However, you can't draw your longsword and then make the opportunity attack with that. From the same page:
To make the opporunity attack, use your reaction to make one melee attack against the provoking creature.
An opportunity attack gives you a single melee attack, and that's it.
You might be thinking of the free object interaction you get on your turn, from the "Other Activity on Your Turn" section on page 190. But as the title says, this is something you get on your turn, not anytime you act or attack in combat. If you made an opportunity attack on your turn, you would technically be able to draw a sword as part of it. But when you get to make an opportunity attack outside of your turn, you get one melee attack, period.