コード例 #1
0
        private void RenewAccessToken()
        {
            StreamReader file         = new StreamReader(Settings.TokenFile);
            var          refreshToken = file.ReadLine();

            file.Close();

            if (string.IsNullOrEmpty(refreshToken))
            {
                var startTime    = DateTime.Now;
                var lastModified = File.GetLastWriteTime(Settings.TokenFile);

                var auth = new AuthorizationModel()
                {
                    ClientId    = Settings.SpotifyClientId,
                    RedirectUri = Settings.RedirectUri,
                    Scope       = "playlist-modify-private",
                };

                auth.DoAuth();

                while (lastModified < startTime)
                {
                    lastModified = File.GetLastWriteTime(Settings.TokenFile);
                    Thread.Sleep(1000);

                    if (DateTime.Now > startTime.AddSeconds(10))
                    {
                        throw new Exception("Timed out waiting for updated access token.");
                    }
                }
            }

            //AuthorizationCodeAuth auth = new AuthorizationCodeAuth(_clientId,
            //                                                       _secretId,
            //                                                       "http://localhost:4002",
            //                                                       "http://localhost:4002",
            //                                                       Scope.PlaylistReadPrivate |
            //                                                       Scope.PlaylistReadCollaborative);
            //auth.AuthReceived += async (sender, payload) =>
            //{

            //}

            //var webAuth = new AuthorizationCodeAuth()
            //{
            //    ClientId = Settings.SpotifyClientId,
            //    RedirectUri = Settings.RedirectUri,
            //    Scope = Scope.PlaylistModifyPrivate,
            //};

            //var token = webAuth.RefreshToken(refreshToken);
            //var token = webAuth.RefreshToken(refreshToken, Settings.SpotifyClientSecret);

            //_accessToken = token

            if (!AccessTokenValid())
            {
                throw new Exception("Failed to renew access token.");
            }
        }