[IMPORTANT] SpotifyLocalAPI not working
With the last version, has worked one time, but no more. Play and pause doesn't work.
@Gruhlum It's probably more coherent to state offset as 0 instead of as an empty string, but because both work I'm assuming it treats an empty string as having no offset anyway. I'm not sure how a string could define some form of offset but who knows
As for your problem, I didn't have the issue and I have Spotify premium, so it's possible, although it wouldn't make much sense since web players for Spotify should allow free users to still interact and play specific tracks. However, IIRC from the free version of Spotify on mobile, you cannot play specific tracks and are forced to play through "shuffle play," meaning if your device ID links to a mobile I suppose it's not going to let you. Again, why would it cause that issue on PausePlayback() though?
@JohnnyCrazy Thank you for linking alternatives to the authentication solution. Creating some form of server could be a really interesting project to tackle so I can get better with that sort of stuff, but it seems potentially tedious. Considering authentication for a user takes only a second with the trade-off of a web browser showing, it's not disastrous for my project, and since I'm mainly making it for myself I guess I don't really care.
Here's to hoping I can actually code a new wrapper for the web API. Thanks Spotify.
@Gruhlum https://developer.spotify.com/documentation/web-api/reference/player/start-a-users-playback/ and https://developer.spotify.com/documentation/web-api/reference/player/pause-a-users-playback/
If the user making the request is non-premium, a 403 FORBIDDEN response code will be returned.
With permium all continues working?
@Gruhlum While @h-philip has showed the reason why it won't work for you, I recently just had an issue with resuming playback and it was because offset was set to 0 instead of an empty string. When I changed it back to an empty string it allowed me to resume playback. @JohnnyCrazy This is a potential bug I think?
If that doc is definitely correct and isn't out of date, then forcing users to be premium is really, REALLY annoying. That's actually terrible. Do you think they disabled the local API just to force users to pay up?
@h-philip Yes I have just found it myself. That's a real bummer looks like I am screwed now :( @rollersteaam before the update I was able to avoid all ads by starting a new song before the last song was finished, guess that's their way of fixing it.
@h-philip In my case I didn't include any contextUri or any song uris, I only provided the offset parameter with a value of "0," so it appears we didn't use it the same way. Both issues definitely could stem from the same problem though.
@Gruhlum While @h-philip has showed the reason why it won't work for you, I recently just had an issue with resuming playback and it was because offset was set to 0 instead of an empty string. When I changed it back to an empty string it allowed me to resume playback. @JohnnyCrazy This is a potential bug I think?
Please have a look at the docs before bug hunting: Resume Playback
Optional. Indicates from where in the context playback should start. Only available when context_uri corresponds to an album or playlist object, or when the uris parameter is used.“position” is zero based and can’t be negative. Example: "offset": {"position": 5}“uri” is a string representing the uri of the item to start at. Example: "offset": {"uri": "spotify:track:1301WleyT98MSxVHPZCA6M"}
@Gruhlum They could've simply checked whether an ad is currently queued and not allowed switching to a different song in that case. But maybe they also wanted to kill ad blockers like mine... Since now (aside from making the local API inaccessible), with the requirement of an API key, I suppose they can simply block keys of offending apps...
@rollersteaam
public void Pause()
{
spotify.PausePlayback();
}
doesn't work for me on no-premium account.
and after call spotify = await webApiFactory.GetWebApi();
visual studio becomes practically frozen
@StepIg Yeah unfortunately if the user making the request is no-premium, they won't fulfill it. I guess that's Spotify's own right, although I find it particularly scummy. Hopefully someone can discover a workaround.
For your frozen issue, calling that function will cause your app to hang until authentication is received or the request times out.
On call, your browser should open up and request for you to authenticate the app's access to your account. You will get an "Invalid URI redirect" message if you did not specify "http://localhost:8000" in your whitelisted addresses in your client dashboard, although if you did not supply your own client ID then you won't have this problem (because it will use JohnnyCrazy's client ID).
Alternatively, you can create a new Task like this:
void ResolveAuth() {
/// bla bla bla enter all the previous constructor code here
/// ...
Task task = Task.Factory.StartNew(() =>
{
try
{
// Since GetWebApi() is async, it returns a Task of return type WebAPIFactory, so we have to use ".Result" to access what the task has returned
spotify = webApiFactory.GetWebApi().Result;
}
catch (Exception ex)
{
// This update status is part of my own class code
UpdateStatus($"Spotify service failed to load, Spotify music is unavailable. Exception: {ex.Message}");
throw;
}
});
}
If you didn't know, tasks create a new thread to handle a task so you can still do other things in your application without it freezing, but of course, you will have null references until you successfully get authentication, so it may not be worth using the Task system if you're expecting a reference to it no matter what.
Despite all that it's still possible because you're non-premium that it fails the authentication request immediately, but I think it would fail loudly and throw an exception if that were the case anyway. You would have to do a lot of null checks throughout your code as well with this task approach, so I opted not to do this and just give the user feedback that I am reaching for authentication before continuing.
@rollersteaam thanks for your help
but don't understand about
"if you did not supply your own client ID then you won't have this problem (because it will use JohnnyCrazy's client ID)."
How to do this?
@Steplg On my phone atm so can't give large examples, but I posted the example of getting authentication further up, and with that spotifywebapi class you can find everything you'll need
Some basic things: GetPlayingTrack returns an object with variable ProgressMs, which is current track time in ms and inside is variable Item with durationms variable which is total track time in ms, as well as info for artist and album
https://developer.spotify.com/community/news/2018/07/19/new-updates-to-spotify-play-button/ Now we know, why they disabled it.
Remote control of Spotify’s desktop app has been deprecated in favour of full track playback in the browser.
Hm - so it looks like there are only two options left, if you need to know what's currently playing, and can't use the WebAPI (for whatever reasons):
-
Use open.spotify.com instead, and write a plugin for your favourite browser. This plugin could parse the DOM of the page. In fact, this would add some missing features the local API had. For instance, it always returned only one artist per song, even if there where multiple of them. The plugin could, e.g. via Websockets, connect to another client.
-
, and this is the more "awesome" approach: tinker around with the Spotify client itself. The client is, in fact, only Chromium + React, and the "spa"-files in the "Apps" folder are zipped files, which can be extracted and modified. There is a project for Rainmeter, which does exactly this: https://github.com/khanhas/Spicetify
So one could implement the "SpotifyWebHelper" as a pure WebSocket service (the aforementioned project has sth. like this). Although it requires tons and tons of tinkering (and a lot of time). BUT - it would be awesome. 😃
@DanielGilbert Done all regarding what playing, elapsed time, etc... Continue looking for play a list and also for make auth without show the browser
both SOLVED
Okay, seems like it's official.
I will think about the future of the project. Probably it will focus on the web API. But I also like the idea of modifying the Spotify client files to make a custom websocket helper. I will keep you updated
can connect using webAPI, but do not accept commands until play is pressed on spotify. After this, commands are accepted by spotify
Try explicitly transferring playback to the desktop client to wake it up (docs here).
Okay, small plan for the future:
-
Remove the local API from this project and make sure we're getting the Web API .NET Standard ready. This was a highly requested feature and without the local API it should be done quite quick.
-
Remove the current Authentication stuff I build around spotify flows and extract it into a new project
SpotifyAPI.Butlerwith a better API, and maybe even cross platform support. I already had a look at a cross platform webserver which looks promising. For the API, I thought of the following:
var butler = new SpotifyAPIButler(clientID, clientSecret, 3000);
// or for short lived tokens
var butler = new SpotifyAPIButler(clientID, 3000);
butler.OnAuthorizationCode+= OnAuthorizationCode; // (token, refreshToken)
butler.OnImplicitGrant += OnImplicitGrant; // (token)
butler.Start() // open users browser
var newToken = butler.RefreshToken(refreshToken) // maybe not even necessary
The API maybe will stay close to the old one, but definitely with better error handling and cross platform support.
And generally speaking: There is no secure way of using the Spotify Web API with long lasting tokens on the client side without a backend. The only option is: Every user has to create a developer application themselves and add the necessary clientId and clientSecret into the Desktop App. That's the only secure way.