Пример #1
0
        //***********************************************************************************************************************************************************************************************************

        /// <summary>
        /// Connect to the Spotify Web API
        /// </summary>
        /// <param name="timeout_ms">Connection timeout in ms</param>
        /// <param name="forceReauthenticate">if true, force the user to reauthenticate to the player application</param>
        /// <returns>true on connection success, otherwise false</returns>
        /// see: https://johnnycrazy.github.io/SpotifyAPI-NET/SpotifyWebAPI/auth/#implicitgrantauth
        /// Use https://developer.spotify.com/dashboard/ to get a Client ID
        /// It should be noted, "http://*****:*****@"(\?|\&|#)([^=]+)\=([^&]+)"); // Extract the fields from the returned URL
                                MatchCollection matches = regex.Matches(urlFinal);
                                foreach (Match match in matches)
                                {
                                    if (match.Value.Contains("access_token"))
                                    {
                                        accessToken = match.Value.Replace("#access_token=", "");
                                    }
                                    else if (match.Value.Contains("token_type"))
                                    {
                                        tokenType = match.Value.Replace("&token_type=", "");
                                    }
                                    else if (match.Value.Contains("expires_in"))
                                    {
                                        ConnectionTokenExpirationTime = new TimeSpan(0, 0, int.Parse(match.Value.Replace("&expires_in=", "")));
                                    }
                                }

                                _spotifyWeb = new SpotifyWebAPI()
                                {
                                    TokenType = tokenType, AccessToken = accessToken
                                };
                                waitForAuthFinish.Set();        // Signal that the authentication finished

                                authWindowClosedByProgram = true;
                                authWindow.Close();
                            }
                            else
                            {
                                authWindow.WindowState = WindowState.Normal;
                                userInteractionWaiting = true;
                            }
                        };

                        authWindow.Closed += (sender, args) =>
                        {
                            waitForWindowClosed.Set();
                            if (!authWindowClosedByProgram)
                            {
                                waitForAuthFinish.Set();
                            }
                        };

                        webBrowser.Navigate(url);       // Navigate to spotifys login page to begin authentication. If credentials exist, you are redirected to an URL containing the access_token.
                        authWindow.ShowDialog();
                    }));
                    newThread.SetApartmentState(ApartmentState.STA);
                    newThread.Start();

                    waitForAuthFinish.WaitOne(timeout_ms);
                    if (userInteractionWaiting)
                    {
                        waitForWindowClosed.WaitOne(); waitForAuthFinish.WaitOne(timeout_ms);
                    }
                }
            });

            if (_spotifyWeb == null)
            {
                IsConnected = false; return(false);
            }
            else
            {
                _wasConnectionTokenExpiredEventRaised = false; IsConnected = true; return(true);
            }
        }