コード例 #1
0
        private async Task <string> GetAccessTokenAsync()
        {
            var credentials = _credentialsRepository.Retrieve();

            if (credentials != null && !string.IsNullOrEmpty(credentials.AccessToken) && credentials.CreateDate.AddSeconds(credentials.ExpiresIn - 10) > DateTime.UtcNow)
            {
                return($"{credentials.TokenType} {credentials.AccessToken}");
            }

            var credentialsId = credentials?.Id;

            switch (_configs["AuthorizationMode"])
            {
            case "InitialConfiguration":
            {
                // IMPORTANT! Use: GetSpotifyAccessTokenCode.html

                credentials = await CreateCredentialsAsync();

                _logger.LogCritical("{Method} Write the following refresh token value to the AppSettings:Spotify:RefreshToken: {RefreshToken}",
                                    MethodBase.GetCurrentMethod().Name, credentials.RefreshToken); // Write this to the RefreshToken field in config file!!!
                break;
            }

            case "RefreshToken":
            {
                credentials = await RefreshCredentialsAsync();

                break;
            }

            default:
            {
                throw new Exception($"{MethodBase.GetCurrentMethod().Name} Invalid AuthorizationMode: {_configs["AuthorizationMode"]} Check settings!");
            }
            }

            credentials.Id = credentialsId;
            _credentialsRepository.Register(credentials);

            return($"{credentials.TokenType} {credentials.AccessToken}");
        }