Getting an authorization code

Once an application is registered an authorization code is required. Obtaining the authorization code gives the end-user the opportunity to grant the application access to a Spotify account. The process is described in the web API guide:

https://developer.spotify.com/web-api/authorization-guide/#authorization-code-flow

Before starting, two assemblies must be imported:

using assembly PresentationFramework 
using assembly System.Web 

The Windows Presentation Framework is used to create a very small interface to load the authorization request page.

A URL must be created that will prompt for authorization:

$authorize = 'https://accounts.spotify.com/authorize?client_id={0}&response_type=code&redirect_uri={1}&scope={2}' -f 
    $clientId, 
    $redirectUrl, 
    'playlist-read-private' 

Scope describes the rights the application would like to have; the user request page will include details of the requested rights. The web API guide contains a list of possible scopes:

https://developer.spotify.com/web-api/using-scopes/

The URL will be added to a WebBrowser control that is displayed to the user:

$window = New-Object System.Windows.Window 
$window.Height = 650 
$window.Width = 450 
$browser = New-Object System.Windows.Controls.WebBrowser 
# Add an event handler to close the window when  
# interaction with Spotify is complete. 
$browser.add_Navigated( {  
if ($args[0].Source -notlike '*spotify*') { 
        $args[0].Parent.Close() 
    } 
} ) 
$browser.Navigate($authorize) 
$window.Content = $browser 
$null = $window.ShowDialog() 

The window will close as soon as it leaves the Spotify pages. That should be when it hits the redirect URL.

If the application has already been authorized the window will close without prompting for user interaction.

The URL it navigates to contains the authorization code in a query string. HttpUtility is used to extract the code from the query string:

$authorizationCode = [System.Web.HttpUtility]::ParseQueryString($window.Content.Source.Query)['code'] 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset