I am developing a .NET application using the SpotifyAPI (I am quite new to C#).
I am getting this error when calling spotify.Player.PausePlayback()
Weirdly I was not getting this error a few weeks ago, even with the version of the code I am sharing in this question, and I have just suddenly started getting it. Here is the version of it that was working and is now giving this weird error:
static (Uri, string) InitializeSpotify()
{
var (verifier, challenge) = PKCEUtil.GenerateCodes();
string ClientId="MyclientId";
var loginRequest = new LoginRequest(new Uri("https://127.0.0.1:8000/callback/"),ClientId,LoginRequest.ResponseType.Code)
{
CodeChallengeMethod = "S256",
CodeChallenge = challenge,
Scope = new[] {Scopes.PlaylistReadPrivate, Scopes.PlaylistReadCollaborative,Scopes.Streaming}
};
var uri = loginRequest.ToUri();
return (uri,verifier);
}
static string GetCode(string url){
Process.Start("C:/Program Files/Google/Chrome/Application/chrome.exe", url);
Console.Write("Write the code here:");
string code = Console.ReadLine();
while (string.IsNullOrEmpty(code)){
Console.WriteLine("You didn't write a code, write it again.");
code = Console.ReadLine();
}
return code;
}
static async Task<SpotifyClient> GetCallback(string code,string verifier)
{
var initialResponse = await new OAuthClient().RequestToken(
new PKCETokenRequest("MyclientId", code, new Uri("https://127.0.0.1:8000/callback/"), verifier)
);
var authenticator = new PKCEAuthenticator("MyclientId", initialResponse);
var config = SpotifyClientConfig.CreateDefault()
.WithAuthenticator(authenticator);
var spotify = new SpotifyClient(config);
Console.Write("Authentication Successful...");
return spotify;
}
static async Task Playback(){
(Uri uri,string verifier)= InitializeSpotify();
string code = GetCode(uri.ToString());
SpotifyClient spotify = await GetCallback(code,verifier);
//Controlling the playback
while (true){
Console.WriteLine();
string state = Console.ReadLine();
if (state=="pause"){await spotify.Player.PausePlayback();}
if (state=="resume"){await spotify.Player.ResumePlayback();}
if (state=="next"){await spotify.Player.SkipNext();}
if (state=="previous"){await spotify.Player.SkipPrevious();}
else{continue;}
}
}
await Playback();
I have tried using other methods and scopes such as
Scopes.UserReadPlaybackState
together with
var playback = await spotify.Player.GetCurrentPlayback();
And that works fine. Which makes me think that maybe modification of the playback state is the problem?
Thanks in advance!