PKCE
The authorization code flow with PKCE is the best option for mobile and desktop applications where it is unsafe to store your client secret. It provides your app with an access token that can be refreshed. For further information about this flow, see IETF RFC-7636.
#
Generating Challenge & VerifierFor every authentication request, a verify code and its challenge code needs to be generated. The class PKCEUtil
can be used to generate those, either with random generated or self supplied values:
#
Generating Login URILike most auth flows, you'll need to redirect your user to Spotify's servers so they are able to grant access to your application:
When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to http://localhost:5000/callback
and a code
parameter is attached to the query. The redirect URI can also contain a custom protocol paired with UWP App Custom Protocol handler. This received code
has to be exchanged for an access_token
and refresh_token
:
With PKCE you can also refresh tokens once they're expired:
If you do not want to take care of manually refreshing tokens, you can use PKCEAuthenticator
: