A former Twitter employee has been sentenced to 3.5 years in prison for spying on behalf of Saudi Arabia.