Spotify.NET/902b04f6.01c2d2c2.js

1 line
8.8 KiB
JavaScript
Raw Normal View History

(window.webpackJsonp=window.webpackJsonp||[]).push([[34],{166:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){return p})),n.d(t,"metadata",(function(){return i})),n.d(t,"rightToc",(function(){return s})),n.d(t,"default",(function(){return l}));var r=n(2),a=n(9),o=(n(0),n(188)),p={id:"token_swap",title:"Token Swap"},i={id:"token_swap",title:"Token Swap",description:"Token Swap provides an authenticatiow flow where client-side apps (like CLI/desktop/mobile apps) are still able to use long-living tokens and the opportunity to refresh them without exposing your application's secret. This however requires a server-side part to work.",source:"@site/docs/token_swap.md",permalink:"/SpotifyAPI-NET/docs/next/token_swap",editUrl:"https://github.com/JohnnyCrazy/SpotifyAPI-NET/edit/master/SpotifyAPI.Docs/docs/token_swap.md",version:"next",lastUpdatedBy:"dependabot[bot]",lastUpdatedAt:1608027439,sidebar:"docs",previous:{title:"PKCE",permalink:"/SpotifyAPI-NET/docs/next/pkce"},next:{title:"Showcase",permalink:"/SpotifyAPI-NET/docs/next/showcase"}},s=[{value:"Flow",id:"flow",children:[]},{value:"Server Implementation",id:"server-implementation",children:[{value:"Swap",id:"swap",children:[]},{value:"Refresh",id:"refresh",children:[]}]},{value:"Example",id:"example",children:[]}],c={rightToc:s};function l(e){var t=e.components,n=Object(a.a)(e,["components"]);return Object(o.b)("wrapper",Object(r.a)({},c,n,{components:t,mdxType:"MDXLayout"}),Object(o.b)("p",null,"Token Swap provides an authenticatiow flow where client-side apps (like CLI/desktop/mobile apps) are still able to use long-living tokens and the opportunity to refresh them without exposing your application's secret. This however requires a server-side part to work."),Object(o.b)("p",null,"It is based on the ",Object(o.b)("a",Object(r.a)({parentName:"p"},{href:"/SpotifyAPI-NET/docs/next/authorization_code"}),"Authorization Code")," flow and is also documented by Spotify: ",Object(o.b)("a",Object(r.a)({parentName:"p"},{href:"https://developer.spotify.com/documentation/ios/guides/token-swap-and-refresh/"}),"Token Swap and Refresh "),"."),Object(o.b)("h2",{id:"flow"},"Flow"),Object(o.b)("p",null,"The client uses the first part of the ",Object(o.b)("inlineCode",{parentName:"p"},"Authorization Code")," flow and redirects the user to Spotify's login page. In this part, only the client id is required. Once the user logged in and confirmed the usage of your app, they will be redirect to a ",Object(o.b)("inlineCode",{parentName:"p"},"http://localhost")," server which grabs the ",Object(o.b)("inlineCode",{parentName:"p"},"code")," from the query parameters."),Object(o.b)("pre",null,Object(o.b)("code",Object(r.a)({parentName:"pre"},{className:"language-csharp"}),'var request = new LoginRequest("http://localhost", "ClientId", LoginRequest.ResponseType.Code)\n{\n Scope = new List<string> { Scopes.UserReadEmail }\n};\nBrowserUtil.Open(uri);\n')),Object(o.b)("p",null,"Now, swapping out this ",Object(o.b)("inlineCode",{parentName:"p"},"code")," for an ",Object(o.b)("inlineCode",{parentName:"p"},"access_token")," would require the app's client secret. We don't have this on the client-side. Instead, we send a request to our server, which takes care of the code swap:"),Object(o.b)("pre",null,Object(o.b)("code",Object(r.a)({parentName:"pre"},{className:"language-csharp"}),'public Task GetCallback(string code)\n{\n var response = await new OAuthClient().RequestToken(\n new TokenSwapTokenRequest("https://your-swap-server.com/swap", code)\n );\n\n var spotify = new SpotifyClient(response.AccessToken);\n // Also important for later: response.RefreshToken\n}\n')),Object(o.b)("p",null,"The server swapped out the ",Object(o.b)("inlineCode",{parentName:"p"},"code")," for an ",Object(o.b)("inlineCode",{parentName:"p"},"access_token")," and ",Object(o.b)("inlineCode",{parentName:"p"},"refresh_token"),". Once we realize the ",Object(o.b)("inlineCode",{parentName:"p"},"access_token")," expired, we can also ask the server to refresh it:"),Object(o.b)("pre",null,Object(o.b)("code",Obj