Spotify.NET/assets/js/e3c15a47.221cdc5d.js

1 line
7.8 KiB
JavaScript
Raw Normal View History

(window.webpackJsonp=window.webpackJsonp||[]).push([[48],{118:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){return i})),n.d(t,"metadata",(function(){return c})),n.d(t,"toc",(function(){return l})),n.d(t,"default",(function(){return p}));var r=n(3),a=n(7),o=(n(0),n(124)),i={id:"pkce",title:"PKCE"},c={unversionedId:"pkce",id:"pkce",isDocsHomePage:!1,title:"PKCE",description:"The authorization code flow with PKCE is the best option for mobile and desktop applications where it is unsafe to store your client secret. It provides your app with an access token that can be refreshed. For further information about this flow, see IETF RFC-7636.",source:"@site/docs/pkce.md",slug:"/pkce",permalink:"/SpotifyAPI-NET/docs/pkce",editUrl:"https://github.com/JohnnyCrazy/SpotifyAPI-NET/edit/master/SpotifyAPI.Docs/docs/pkce.md",version:"current",lastUpdatedBy:"Jonas Dellinger",lastUpdatedAt:1618500336,formattedLastUpdatedAt:"4/15/2021",sidebar:"docs",previous:{title:"Authorization Code",permalink:"/SpotifyAPI-NET/docs/authorization_code"},next:{title:"Token Swap",permalink:"/SpotifyAPI-NET/docs/token_swap"}},l=[{value:"Generating Challenge & Verifier",id:"generating-challenge--verifier",children:[]},{value:"Generating Login URI",id:"generating-login-uri",children:[]}],s={toc:l};function p(e){var t=e.components,n=Object(a.a)(e,["components"]);return Object(o.b)("wrapper",Object(r.a)({},s,n,{components:t,mdxType:"MDXLayout"}),Object(o.b)("blockquote",null,Object(o.b)("p",{parentName:"blockquote"},"The authorization code flow with PKCE is the best option for mobile and desktop applications where it is unsafe to store your client secret. It provides your app with an access token that can be refreshed. For further information about this flow, see ",Object(o.b)("a",{parentName:"p",href:"https://tools.ietf.org/html/rfc7636"},"IETF RFC-7636"),".")),Object(o.b)("h2",{id:"generating-challenge--verifier"},"Generating Challenge & Verifier"),Object(o.b)("p",null,"For every authentication request, a verify code and its challenge code needs to be generated. The class ",Object(o.b)("inlineCode",{parentName:"p"},"PKCEUtil")," can be used to generate those, either with random generated or self supplied values:"),Object(o.b)("pre",null,Object(o.b)("code",{parentName:"pre",className:"language-csharp"},'// Generates a secure random verifier of length 100 and its challenge\nvar (verifier, challenge) = PKCEUtil.GenerateCodes();\n\n// Generates a secure random verifier of length 120 and its challenge\nvar (verifier, challenge) = PKCEUtil.GenerateCodes(120);\n\n// Returns the passed string and its challenge (Make sure it\'s random and long enough)\nvar (verifier, challenge) = PKCEUtil.GenerateCodes("YourSecureRandomString");\n')),Object(o.b)("h2",{id:"generating-login-uri"},"Generating Login URI"),Object(o.b)("p",null,"Like most auth flows, you'll need to redirect your user to Spotify's servers so they are able to grant access to your application:"),Object(o.b)("pre",null,Object(o.b)("code",{parentName:"pre",className:"language-csharp"},'// Make sure "http://localhost:5000/callback" is in your applications redirect URIs!\nvar loginRequest = new LoginRequest(\n new Uri("http://localhost:5000/callback"),\n "YourClientId",\n LoginRequest.ResponseType.Code\n)\n{\n CodeChallengeMethod = "S256",\n CodeChallenge = challenge,\n Scope = new[] { Scopes.PlaylistReadPrivate, Scopes.PlaylistReadCollaborative }\n};\nvar uri = loginRequest.ToUri();\n// Redirect user to uri via your favorite web-server or open a local browser window\n')),Object(o.b)("p",null,"When the user is redirected to the generated uri, they will have to login with their Spotify account and confirm that your application wants to access their user data. Once confirmed, they will be redirected to ",Object(o.b)("inlineCode",{parentName:"p"},"http://localhost:5000/callback")," and a ",Object(o.b)("inlineCode",{parentName:"p"},"code")," parameter is attached to the query. The redirect URI can also contain a custom protocol paired with UWP App Custom Protocol handler. This received "