(window.webpackJsonp=window.webpackJsonp||[]).push([[7],{135:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){returni})),n.d(t,"metadata",(function(){returnc})),n.d(t,"rightToc",(function(){returns})),n.d(t,"default",(function(){returnp}));varr=n(2),o=n(9),a=(n(0),n(186)),i={id:"authorization_code",title:"Authorization Code"},c={id:"version-5.1.1/auth/authorization_code",title:"Authorization Code",description:"This way is not recommended for client-side apps and requires server-side code to run securely.",source:"@site/versioned_docs/version-5.1.1/auth/authorization_code.md",permalink:"/SpotifyAPI-NET/docs/auth/authorization_code",editUrl:"https://github.com/JohnnyCrazy/SpotifyAPI-NET/edit/master/SpotifyAPI.Docs/versioned_docs/version-5.1.1/auth/authorization_code.md",version:"5.1.1",lastUpdatedBy:"Jonas Dellinger",lastUpdatedAt:1591664691,sidebar:"version-5.1.1/someSidebar",previous:{title:"Implicit Grant",permalink:"/SpotifyAPI-NET/docs/auth/implicit_grant"},next:{title:"Client Credentials",permalink:"/SpotifyAPI-NET/docs/auth/client_credentials"}},s=[{value:"Token Refresh",id:"token-refresh",children:[]}],u={rightToc:s};functionp(e){vart=e.components,n=Object(o.a)(e,["components"]);returnObject(a.b)("wrapper",Object(r.a)({},u,n,{components:t,mdxType:"MDXLayout"}),Object(a.b)("p",null,"This way is ",Object(a.b)("strong",{parentName:"p"},"not recommended")," for client-side apps and requires server-side code to run securely.\nWith this approach, you first get a code which you need to trade against the access-token.\nIn this exchange you need to provide your Client-Secret and because of that it's not recommended.\nA good thing about this method: You can always refresh your token, without having the user to auth it again."),Object(a.b)("p",null,"More info: ",Object(a.b)("a",Object(r.a)({parentName:"p"},{href:"https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow"}),"here")),Object(a.b)("pre",null,Object(a.b)("code",Object(r.a)({parentName:"pre"},{className:"language-csharp"}),'static async void Main(string[] args)\n{\n AuthorizationCodeAuth auth = new AuthorizationCodeAuth(\n _clientId,\n _secretId,\n "http://localhost:4002",\n "http://localhost:4002",\n Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative\n );\n\n auth.AuthReceived += async (sender, payload) =>\n {\n auth.Stop();\n Token token = await auth.ExchangeCode(payload.Code);\n SpotifyWebAPI api = new SpotifyWebAPI()\n {\n TokenType = token.TokenType,\n AccessToken = token.AccessToken\n };\n // Do requests with API client\n };\n auth.Start(); // Starts an internal HTTP Server\n auth.OpenBrowser();\n}\n')),Object(a.b)("h2",{id:"token-refresh"},"Token Refresh"),Object(a.b)("p",null,"Once the ",Object(a.b)("inlineCode",{parentName:"p"},"AccessToken")," is expired, you can use your ",Object(a.b)("inlineCode",{parentName:"p"},"RefreshToken")," to get a new one.\nIn this procedure, no HTTP Server is needed in the background and a single HTTP Request is made."),Object(a.b)("pre",null,Object(a.b)("code",Object(r.a)({parentName:"pre"},{className:"language-csharp"}),"// Auth code from above\n\nif(token.IsExpired())\n{\n Token newToken = await auth.RefreshToken(token.RefreshToken);\n api.AccessToken = newToken.AccessToken\n api.TokenType = newToken.TokenType\n}\n")))}p.isMDXComponent=!0},186:function(e,t,n){"use strict";n.d(t,"a",(function(){returnd})),n.d(t,"b",(function(){returnf}));varr=n(0),o=n.n(r);functiona(e,t,n){returntine?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}functioni(e,t){varn=Object.keys(e);if(Object.getOwnPropertySymbols){varr=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){returnObject.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}returnn}functionc(e){for(vart=1;t<arguments.length;t++){varn=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){a(e,t,n[t])}