To be honest, in all my M2TWing I never had the AI open my gates with a spy. Ever.

However, to catch a spy, especially a high level one, you want to train your spies to a higher subterfuge level. A lot of low levels can be useful, yes, but each level means a lot more and its easier to stay inside a town than it is to infiltrate. So you can use your 20 spies to spy on nearby Danish armies before sending them back to defend.

Also, having a general with high public security helps. That involves being around when a spy dies in your town, so its not overly common.

Outside of that, take the bordering Danish towns and they should normally remove the spy. I think the AI has a tendency to only infiltrate bordering settlements.