Пример #1
0
        private string GetSessionKey()
        {
            const string sessionKeyRegistryKeyName = "LastFmSessionKey";

            // try get the session key from the registry
            string sessionKey = GetRegistrySetting(sessionKeyRegistryKeyName, null);

            if (string.IsNullOrEmpty(sessionKey))
            {
                // instantiate a new scrobbler
                var scrobbler = new Scrobbler(apiKey, apiSecret);

                //NOTE: This is demo code. You would not normally do this in a production application
                while (string.IsNullOrEmpty(sessionKey))
                {
                    // Try get session key from Last.fm
                    try {
                        sessionKey = scrobbler.GetSession();

                        // successfully got a key. Save it to the registry for next time
                        SetRegistrySetting(sessionKeyRegistryKeyName, sessionKey);
                    } catch (LastFmApiException exception) {
                        // get a url to authenticate this application
                        string url = scrobbler.GetAuthorisationUri();

                        // open the URL in the default browser
                        Process.Start(url);
                        //Console.ReadKey();
                    }
                }
            }

            return(sessionKey);
        }
Пример #2
0
        private Session GetSession()
        {
            // try get the session key from the registry
            Session session = new Session();

            Properties.Settings.Default.Upgrade();

            session.Key      = Properties.Settings.Default.SessionKey;
            session.Username = Properties.Settings.Default.UserName;

            if (string.IsNullOrEmpty(session.Key) || string.IsNullOrEmpty(session.Username))
            {
                // instantiate a new scrobbler
                var scrobbler = new Scrobbler(ApiKey, ApiSecret, new Session()
                {
                    Key = session.Key
                });

                //NOTE: This is demo code. You would not normally do this in a production application
                while (string.IsNullOrEmpty(session.Key) || string.IsNullOrEmpty(session.Username))
                {
                    // Try get session key from Last.fm
                    try
                    {
                        session = scrobbler.GetSession();

                        // successfully got a key. Save it to the registry for next time
                        Properties.Settings.Default.SessionKey = session.Key;
                        Properties.Settings.Default.UserName   = session.Username;
                        Properties.Settings.Default.Save();
                    }
                    catch (LastFmApiException)
                    {
                        // get session key from Last.fm failed
                        //MessageBox.Show(exception.Message);
                        if (MessageBox.Show(
                                "Linear Audio Playerはlast.fmに対応しています。last.fmを使用しますか?\nはいを選択した場合はlast.fmのアカウントへの認証設定のページを開きます。", "use last.fm?", MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            return(null);
                        }

                        // get a url to authenticate this application
                        string url = scrobbler.GetAuthorisationUri();

                        // open the URL in the default browser
                        Process.Start(url);

                        // Block this application while the user authenticates
                        MessageBox.Show("ブラウザで許可したあとOKボタンをクリックしてください。");
                    }
                }
            }

            return(session);
        }
Пример #3
0
        public void GetAuthorisationUri_TestApiKey_ReturnsWellFormedUri()
        {
            // Arrange
            var    scrobbler      = new Scrobbler(ApiKey, ApiSecret);
            string expectedResult = string.Format(Scrobbler.RequestAuthorisationUriPattern, ApiKey, "");

            // Act
            string uri = scrobbler.GetAuthorisationUri();

            // Assert
            Assert.IsTrue(uri.StartsWith(expectedResult));
        }
        public void GetAuthorisationUri_TestApiKey_ReturnsWellFormedUri()
        {
            // Arrange
            var scrobbler = new Scrobbler(ApiKey, ApiSecret);
            string expectedResult = string.Format(Scrobbler.RequestAuthorisationUriPattern, ApiKey, "");

            // Act
            string uri = scrobbler.GetAuthorisationUri();

            // Assert
            Assert.IsTrue(uri.StartsWith(expectedResult));
        }
Пример #5
0
        public void GetSession_AuthorisedToken_ReturnsSessionKey()
        {
            // Arrange
            var scrobbler = new Scrobbler(ApiKey, ApiSecret);

            // break here and authorise via a browser
            string uri = scrobbler.GetAuthorisationUri();

            // Act
            string session = scrobbler.GetSession();

            // Assert
            Assert.IsNotNull(session);
        }
        public void GetSession_AuthorisedToken_ReturnsSessionKey()
        {
            // Arrange
            var scrobbler = new Scrobbler(ApiKey, ApiSecret);

            // break here and authorise via a browser
            string uri = scrobbler.GetAuthorisationUri();

            // Act
            string session = scrobbler.GetSession();

            // Assert
            Assert.IsNotNull(session);
        }
Пример #7
0
        public static void Authorize()
        {
            // instantiate a new scrobbler
            if (scrobbler == null)
            {
                scrobbler = new Scrobbler(ApiKey, ApiSecret);
            }

            // get a url to authenticate this application
            string url = scrobbler.GetAuthorisationUri();

            // open the URL in the default browser
            Process.Start(url);
        }
Пример #8
0
        private string GetSessionKey()
        {
            LoadSettings();

            // try get the session key from the registry
            string sessionKey = appSettings.Key;

            if (string.IsNullOrEmpty(sessionKey))
            {
                // instantiate a new scrobbler
                var scrobbler = new Scrobbler(ApiKey, ApiSecret);

                //NOTE: This is demo code. You would not normally do this in a production application
                while (string.IsNullOrEmpty(sessionKey))
                {
                    // Try get session key from Last.fm
                    try {
                        sessionKey = scrobbler.GetSession();

                        // successfully got a key. Save it to the registry for next time
                        appSettings.Key = sessionKey;
                        SaveSettings();
                        //SetRegistrySetting(sessionKeyRegistryKeyName, sessionKey);
                    }
                    catch (LastFmApiException exception) {
                        // get session key from Last.fm failed
                        //    Console.WriteLine(exception.Message);

                        // get a url to authenticate this application
                        string url = scrobbler.GetAuthorisationUri();

                        // open the URL in the default browser
                        Process.Start(url);

                        // To enable easy authorisation controlled by other application, this must be the first response.
                        // An external application can check whether authentication is needed by checking first responce to the message below
                        Console.WriteLine("[Authenticates] Press RETURN when application authenticated.");

                        Console.ReadLine();
                    }
                }
            }

            return(sessionKey);
        }
Пример #9
0
        /// <summary>
        /// Initialize scrobbler session for safire
        /// </summary>
        public static void Initialize()
        {
            try
            {
                if (Settings.Default.Scrobble && Core.Connectivity.CheckForInternetConnection())
                {
                    string sessionKey = GetRegistrySetting(sessionKeyRegistryKeyName, null);

                    if (string.IsNullOrEmpty(sessionKey))
                    {
                        var mw = App.Current.MainWindow as MainWindow;
                        mw.MetroDialogOptions.ColorScheme = MetroDialogColorScheme.Theme;

                        var mySettings = new MetroDialogSettings()
                        {
                            AffirmativeButtonText = "Done",
                            NegativeButtonText    = "Cancel",

                            ColorScheme = MetroDialogColorScheme.Theme
                        };
                        ThreadPool.QueueUserWorkItem(state =>
                        {
                            // instantiate a new scrobbler
                            // get a url to authenticate this application
                            string url = scrobbler.GetAuthorisationUri();
                            // open the URL in the default browser
                            Process.Start(url);
                        });
                        var result =
                            UserRequest.ShowDialogBox(
                                "To connect Safire to your last.fm account, follow the instructions given in the page that just opened up in your browser. Click \'Done\' only after you allow access.",
                                "Done", "Cancel", "Scrobbler");

                        if (result == ConfirmResult.Affirmative)
                        {
                            ThreadPool.QueueUserWorkItem(_ => GetSessionKey());
                        }
                    }
                }
            }
            catch
            {
            }
            Player.Instance.PropertyChanged += Instance_PropertyChanged;
        }