Extract obtaining an access token to a separate class.

Reduces code duplication and shows the logic in one place.