2020-08-06 19:48:49 +01:00
---
id: pkce
title: PKCE
---
2020-09-16 08:13:21 +01:00
> 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](https://tools.ietf.org/html/rfc7636).
2020-08-06 19:48:49 +01:00
## Generating Challenge & Verifier
2020-09-16 08:13:21 +01:00
For 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:
2020-08-06 19:48:49 +01:00
```csharp
// Generates a secure random verifier of length 100 and its challenge
var (verifier, challenge) = PKCEUtil.GenerateCodes();
// Generates a secure random verifier of length 120 and its challenge
var (verifier, challenge) = PKCEUtil.GenerateCodes(120);
2020-08-19 13:03:01 +01:00
// Returns the passed string and its challenge (Make sure it's random and long enough)
2020-08-06 19:48:49 +01:00
var (verifier, challenge) = PKCEUtil.GenerateCodes("YourSecureRandomString");
```
## Generating Login URI
2020-09-16 08:13:21 +01:00
Like most auth flows, you'll need to redirect your user to Spotify's servers so they are able to grant access to your application:
2020-08-06 19:48:49 +01:00
```csharp
2023-05-27 20:42:31 +01:00
// Make sure "http://localhost:5543/callback" is in your applications redirect URIs!
2020-08-06 19:48:49 +01:00
var loginRequest = new LoginRequest(
2023-05-27 20:42:31 +01:00
new Uri("http://localhost:5543/callback"),
2020-08-06 19:48:49 +01:00
"YourClientId",
LoginRequest.ResponseType.Code
)
{
CodeChallengeMethod = "S256",
CodeChallenge = challenge,
Scope = new[] { Scopes.PlaylistReadPrivate, Scopes.PlaylistReadCollaborative }
};
var uri = loginRequest.ToUri();
2020-08-19 13:03:01 +01:00
// Redirect user to uri via your favorite web-server or open a local browser window
2020-08-06 19:48:49 +01:00
```
2023-05-27 20:42:31 +01:00
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:5543/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` :
2020-08-06 19:48:49 +01:00
```csharp
2023-05-27 20:42:31 +01:00
// This method should be called from your web-server when the user visits "http://localhost:5543/callback"
2020-08-06 19:48:49 +01:00
public Task GetCallback(string code)
{
// Note that we use the verifier calculated above!
2020-08-19 13:03:01 +01:00
var initialResponse = await new OAuthClient().RequestToken(
2023-05-27 20:42:31 +01:00
new PKCETokenRequest("ClientId", code, "http://localhost:5543", verifier)
2020-08-06 19:48:49 +01:00
);
2020-08-19 13:03:01 +01:00
var spotify = new SpotifyClient(initialResponse.AccessToken);
2020-08-06 19:48:49 +01:00
// Also important for later: response.RefreshToken
}
```
With PKCE you can also refresh tokens once they're expired:
```csharp
2020-08-19 13:03:01 +01:00
var newResponse = await new OAuthClient().RequestToken(
new PKCETokenRefreshRequest("ClientId", initialResponse.RefreshToken)
2020-08-06 19:48:49 +01:00
);
2020-08-19 13:03:01 +01:00
var spotify = new SpotifyClient(newResponse.AccessToken);
2020-08-06 19:48:49 +01:00
```
2020-08-19 13:03:01 +01:00
If you do not want to take care of manually refreshing tokens, you can use `PKCEAuthenticator` :
2020-08-06 19:48:49 +01:00
2020-08-19 13:03:01 +01:00
```csharp
var authenticator = new PKCEAuthenticator(clientId, initialResponse);
var config = SpotifyClientConfig.CreateDefault()
.WithAuthenticator(authenticator);
var spotify = new SpotifyClient(config);
```