SpotifyAPI-NET icon indicating copy to clipboard operation
SpotifyAPI-NET copied to clipboard

SpotifyAPI.Web 3.0 Release

Open JohnnyCrazy opened this issue 8 years ago • 31 comments

Hey guys,

Changes in 3.0.0:

  • .net standard support
  • Seperated NuGet Packages for Core Client and Auth flows:
  • Auth flows now use a really good webserver called embedio, no hacky internal solution
  • Auth flows now can serve HTML/static files, so we could build a really good user experience
  • Auth flows have a cleaner API and should be more stable
  • Docs are not updated yet. But the API is very similar to the old one.

Till the docs are updated, feel free to ask stuff.

JohnnyCrazy avatar Aug 24 '18 13:08 JohnnyCrazy

@JohnnyCrazy @BrianLima I've created a gist with everything you need to know about php files, html files and usage of AuthorizationCodeAuth to obtain a 'full' flow of secure authentication for the Spotify API.

https://gist.github.com/rollersteaam/26bf07f99f9ef768a1c71c4f61fb66a4

It's worth noting you CANNOT use GitHub Pages to host your php files, you must get a free or paid web server that supports php scripts to do it, otherwise all of your php code will not only not work, but display as normal HTML and expose what you set your CLIENT_SECRET to.

This can be made a lot better probably? It's possible we can also make changes to SpotifyAPI-NET to also better support this use case based off of what I've had to do here, although I haven't thought that deeply into it.

The only thing missing is getting a refreshed access token. It will follow a similar process to what is already shown, and I'll likely release the code for that when I've done it. But everything shown in that gist will get you your valuable access token and using the API right away - it's just that I didn't code the 'refresh' way. I'll update my gist and put a post here when I get around to it.

EDIT: It's worth noting that because of some improvements I made to the header() calls and window.location, your client_id and redirect_uri are completely hidden from a user's browsing history or address bar. The only possible way they could find them out is by packet sniffing their own connection as THEIR web browser is the one making the GET request to Spotify, despite that, even if they are both compromised it's not a problem. The client secret is never exposed in the process.

rollersteaam avatar Sep 12 '18 15:09 rollersteaam

I'm on a roll, I've updated the git, here's the link to it again, and this time I've added refresh_token to index.php and therefore the ability to request refreshed access tokens using your php server. I've also implemented a timer that will automatically refresh the current access token silently, meaning that no browser will be opened up on refresh. That means that the only time in the whole process the browser is opened up is on program init.

That's my wild journey into php and HTTP request garbage OVER. Hopefully I do not have to touch these topics of programming again for maybe a year or so. This took me basically 15 hours to do in total. Consider this my payback for creating a lovely API :)

rollersteaam avatar Sep 12 '18 16:09 rollersteaam

I've updated the gist again... This time I changed the code to block execution until authentication is received or it times out after 10 seconds of waiting. Very useful if you're expecting authentication to be gained right after calling that function. What should be improved about it is marking ResolveAuth as async and returning a Task, as well as encompassing the function body with ANOTHER Task so that people can choose to await the task or let it run in the background. But damn, this has come a long damn way. I've solved all the problems that I've had with the SpotifyAPI-NET, and now there is the safe auth method that everyone can use readily available now. @JohnnyCrazy Do you think you could take a look at the gist and decide on whether you want to add it to your wiki or not? And if so, would you consider making a class that would suit this use case?

EDIT: It's possible I may make a pull request with some additions, but I'm not sure if I have the time to do it.

rollersteaam avatar Sep 12 '18 17:09 rollersteaam

I've completed the new authorization method. I've created a pull request detailing the additions, the new classes are called SecureAuthorizationCodeAuth and SecureWebAPIFactory. SecureWebAPIFactory follows in the same footprint as the WebAPIFactory in 2.0.0, it's really easy to use with some very useful new features such as auto refreshing, access expiry events and auth success and failure events. SecureAuthorizationCodeAuth itself has silent refreshing for its access.

Creating your own exchange server is incredibly easy as well, it's quite literally just 2 php files into your php web server, input your necessary client id, secret, redirect uri, etc. in all the different files and you're good to go. You now have secured your client secret, you never need to open the user's browser to refresh access, and you can get your access automatically refreshed.

rollersteaam avatar Sep 14 '18 02:09 rollersteaam

@rollersteaam I'm trying to use your pull request and there is a little error with HtmlResponse. In SecureWebAPIFactory, the property HtmlResponse is never initialized to "", so when later on you check for HtmlResponse != "", you overwrite the default HtmlResponse with null. By the way, you should use string.IsNullOrEmpty method instead of a comparison with "".

OmiCron07 avatar Oct 20 '18 15:10 OmiCron07

@rollersteaam I'm trying to use your pull request and there is a little error with HtmlResponse. In SecureWebAPIFactory, the property HtmlResponse is never initialized to "", so when later on you check for HtmlResponse != "", you overwrite the default HtmlResponse with null. By the way, you should use string.IsNullOrEmpty method instead of a comparison with "".

@OmiCron07 Oh god that's bad, didn't realise strings were nullable. Thanks for the assist, was trying to rapidly implement a feature in my app so had to change my fork haphazardly, would have never picked up on that since I change html response. Hope your implementation using the fork is going well, if you ever need help I updated the wiki entry for authorization on my fork which details the process quite a bit.

rollersteaam avatar Oct 20 '18 17:10 rollersteaam

@rollersteaam Yeah, found the nice documentation and for now, is working great.

OmiCron07 avatar Oct 20 '18 18:10 OmiCron07

Is there any plans to release a newer version on nuget with this fix built in: 431 response when writing

ceriwestcott avatar Nov 22 '18 12:11 ceriwestcott

Sorry, Yes, will follow in some hours 😀

JohnnyCrazy avatar Nov 22 '18 16:11 JohnnyCrazy

@JohnnyCrazy Nice! Thank you :+1:

ceriwestcott avatar Nov 22 '18 17:11 ceriwestcott

@ceriwestcott 3.1.1 is rolled out and should be on NuGet soon, thanks for the heads-up :)

JohnnyCrazy avatar Nov 22 '18 19:11 JohnnyCrazy