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

Assuming I need the AuthorizationCodeAuth class to now do my auth, what do I fill in for the serverUri parameter?

Nensec avatar Aug 24 '18 14:08 Nensec

Are there any plans to integrate a way to have events again? This is specifically handy for recording programs that use this API.

mmartain avatar Aug 24 '18 14:08 mmartain

@Nensec

The serverUri parameter will be used as the listen address. In the example I'm using http://localhost:4002.

JohnnyCrazy avatar Aug 24 '18 14:08 JohnnyCrazy

@mmartin

Not planned, since events don't make sense when using a REST API. We would need websocket support from spotify to cleanly implement it. But I guess you could roll your own polling and event trigger logic.

JohnnyCrazy avatar Aug 24 '18 14:08 JohnnyCrazy

Thanks very much for your job! Then, do you think there is a way to manage track change event without calling endpoint continously?

StepIg avatar Aug 24 '18 15:08 StepIg

@JohnnyCrazy you said u experimented with javascript hijacking. Are there any samples left using that method for the tinkerers?

mmartain avatar Aug 24 '18 15:08 mmartain

I've updated nuget package to 3.0.1 and i'm not sure, but i think i don't have Auth namespace http://prntscr.com/kmlg4g (i had this namespace used, but now it's lost).

I've tried to reinstall this nuget package, but it didn't help... And this is nuget screenshot: http://prntscr.com/kmlh9o

Is this my fault or nuget problem?

jasniec avatar Aug 24 '18 23:08 jasniec

@jasniec Did you actually read the top post? 😄

JohnnyCrazy avatar Aug 24 '18 23:08 JohnnyCrazy

Ok, sorry - i'm blind - that's what happens when you try to think in the middle of the night

jasniec avatar Aug 25 '18 08:08 jasniec

When I am doing the auth.Start() and auth.OpenBrowser in a client, is there a way to only give it the ClientId, and then let the server (within AuthReceived) add the SecretId before ExchangeCode()? When I am not setting the SecretId before auth.Start(); auth.OpenBrowser() I always have to enter everything manually, i think. Or do I have to just give it anything as SecretId and then enter the right one before ExchangeCode()?

Maybe I am also doing something really wrong here :smile:

h-philip avatar Aug 25 '18 17:08 h-philip

Good point @h-philip, maybe we should change the API to explicitly state if you want to let the user enter clientId and clientSecret. Currently, I'm just checking wether one of both values is empty or null.

Your proposed solution should work, but I guess we need to change it for some use-cases!

Out of curiosity, why dont you have the client secret when opening the browser? 😅

JohnnyCrazy avatar Aug 25 '18 21:08 JohnnyCrazy

I just didn't want to have to enter the two things myself every time. So if the client has any placeholder as secret, this can be changed after the AuthReceived-event, and you don't have to enter anything in the browser itself and at the same time the secret is not stored in the client, but can remain with the server, which also receives the code.

h-philip avatar Aug 25 '18 23:08 h-philip

So, in the end there's no secure way to use the Spotify API and be secure on a standalone desktop app? All the ways to store the secretId can be broken, specially for open source apps.

BrianLima avatar Sep 08 '18 16:09 BrianLima

If you don't want to have server-side code and keep ur secretId secret, I think the Implicit Grant Flow is the best way.

h-philip avatar Sep 08 '18 21:09 h-philip

Sadly this is not a complete secure solution either @h-philip . You would have to register localhost as a redirect URI, and this allows everyone to generate tokens since everyone "controls" this domain.

The only completely secure alternative, besides running a Backend Server, is to force the client to register a new developer app. This way, everyone has its own secret and client IDs. This also allows using authorization code auth for long lively tokens. However, spotify made it a real pain to generate new apps, so it's not very practical...

JohnnyCrazy avatar Sep 08 '18 22:09 JohnnyCrazy

Thanks for the insight guys. I think i might drop the Spotify integration altogether for my project in the end and try to use a more broad Windows Media Playback API. It might even be more suited.

BrianLima avatar Sep 09 '18 13:09 BrianLima

@BrianLima What are you talking about as alternative?

StepIg avatar Sep 10 '18 06:09 StepIg

@StepIg He's most likely just saying he'll use local files stored on the user's computer as part of the music in his project instead, although I'm just making guesses since AFAIK Windows Media Playback can only do that.

@JohnnyCrazy First off, I just wanted to thank you for creating this library and all the time you've put into it. You brought my project to life and it's very unlikely I would have got this far without your help. Thank you. I think my project is near release but it's closed source (but free), do you only showcase open source projects on your documentation page?

And for my second question, do you have examples of this backend server that would do the key exchange? I'm not sure what the process of the key exchange is, but after thinking about this for a while I might be open to the idea of trying to roll my own php script that I'll probably just hoist on github pages, but only if I could understand the basics of what the php would be returning. Not saying do the entire thing for me (unless you're open to the challenge obviously) but I would be very grateful for a few pointers if you know of some.

My ultimate goal is to eliminate requiring opening the browser to refresh authentication.

rollersteaam avatar Sep 11 '18 19:09 rollersteaam

@rollersteaam you are right, but that will not solve the problem anyway.

BrianLima avatar Sep 11 '18 19:09 BrianLima

After 11 hours straight working on a solution to my own problem of refreshing authentication without re-opening the browser, securing the client secret and figuring out what 'performing a key change' on a 'backend' server actually meant, I have finally reached the end. The golden pot at the end of the rainbow.

I managed to hack enough of @JohnnyCrazy 's new 3.0.0 AuthorizationCodeAuth class to obtain user permissions initially by opening the browser, but by defining my own redirect URI that links to an auth folder on my website, I can redirect the start.html via meta tag refresh to my start.php, where I construct a GET request to the Spotify API. The GET request has the redirect URI that I link to it in start.php, so when it returns the authentication code it links to a safe, whitelisted page hosted on my website. Then I construct a meta refresh tag on that page with php and send along the authentication code to http://localhost:4002 via redirect, where SpotifyAPI's local server is still listening to the owls and crickets.

I didn't provide a unique state parameter however, that was just 123 for now, may add that along the request pipeline eventually as its kind of important security wise... And for scopes, I didn't pass those along either. I debugged Scope.GetStringAttributes(" ") on program init and copied the string representation of the scopes I had already previously defined in the object constructor of AuthorizationCodeAuth, and pasted them into the authorize GET request to Spotify. Hacky, but what the hell isn't hacky about any of this solution?

But the big goal was getting it TO the localhost:4002. This opened up the big iron door blocking the way: getting from "browsing" web browser pages back to my application with the auth code from Spotify intact. Now the authentication received event was fired, I can begin to prepare phase 2, the handshake. Using HTTPClient I make a post request with the response code to, again, a php file hosted on my website which uses curl and inits to the spotify token api. The magic of it is in the server-side only execution, the client secret is defined as a constant, but only to my server. So I can construct the header requirements with the base64 encode perfectly with no user involvement whatsoever. Then I just add the normal auth code to the post body and send it along. When the result is returned, the HTTPClient automatically receives the message and parses it from json to the Token object already provided by SpotifyAPI Models. Boom. That. Is. It. I've got my access token. I've got my refresh token. I've got my 1 hour long expire times. I've got my client secret locked away and out of sight. And the process is completely seamless. Now getting there and setting it up was NOT seamless, but for the user its like any other day.

Now don't get me wrong. I'm sure there is MUCH MORE efficient ways to do this, but it's 7am, I haven't slept, and I've worked for 11 hours straight learning php for the first time and learning the avalanche of cheesecake that http requests are, but I will post code tomorrow when I actually sleep because I'm dying as I type this.

Main goals now are finding out how to halt execution of my program while waiting for the AuthReceived event to fire and setting up an automatic refresh authentication system/timer which I think I know how to refresh without use of the browser opening. After that I have completely finished my integration with Spotify and have all issues clear from my plate. Tomorrow's sounding pretty good to me.

rollersteaam avatar Sep 12 '18 06:09 rollersteaam