Exemple #1
0
        private void RefreshTokenJob()
        {
            _logger.LogInformation($"Adding refresh token job");
            _schedulerService.AddJob(async() =>
            {
                _logger.LogInformation("Refresh token");

                var res = await _httpClient.PostAsync(TokenAuthUrl,
                                                      HttpClientUtils.BuildFormParams(
                                                          new KeyValuePair <string, string>("grant_type", "refresh_token"),
                                                          new KeyValuePair <string, string>("refresh_token", _config.RefreshToken),
                                                          new KeyValuePair <string, string>("client_id", _config.ClientId),
                                                          new KeyValuePair <string, string>("client_secret", _config.ClientSecret)));
                var st = await res.Content.ReadAsStringAsync();

                var newToken        = st.FromJson <OAuthTokenResult>();
                _config.AccessToken = newToken.AccessToken;
                _config.ExpireOn    = DateTime.Now.AddSeconds(newToken.ExpiresIn);
                _componentsService.SaveComponentConfig(_config);

                _logger.LogInformation($"Token refresh expire on: {_config.ExpireOn}");

                InitSpotifyClient();
            }, "SpotifyRefreshToken", (int)TimeSpan.FromMinutes(30).TotalSeconds, false);
        }
Exemple #2
0
        public async void OnOAuthResult(OAuthResult result)
        {
            if (!string.IsNullOrEmpty(result.Code))
            {
                var res = await _httpClient.PostAsync(TokenAuthUrl,
                                                      HttpClientUtils.BuildFormParams(
                                                          new KeyValuePair <string, string>("grant_type", "authorization_code"),
                                                          new KeyValuePair <string, string>("grant_type", "authorization_code"),
                                                          new KeyValuePair <string, string>("code", result.Code),
                                                          new KeyValuePair <string, string>("client_id", _config.ClientId),
                                                          new KeyValuePair <string, string>("client_secret", _config.ClientSecret),
                                                          new KeyValuePair <string, string>("redirect_uri", RedirectUrl)
                                                          ));


                if (res.StatusCode == HttpStatusCode.OK)
                {
                    var tokenString = await res.Content.ReadAsStringAsync();

                    var tokenInfo = tokenString.FromJson <OAuthTokenResult>();
                    _logger.LogInformation(
                        $"Spotify authentication OK, token expire {DateTime.Now.AddSeconds(tokenInfo.ExpiresIn).ToString()}");
                    _config.AccessToken  = tokenInfo.AccessToken;
                    _config.TokenType    = tokenInfo.TokenType;
                    _config.ExpireOn     = DateTime.Now.AddSeconds(tokenInfo.ExpiresIn).ToLocalTime();
                    _config.RefreshToken = tokenInfo.RefreshToken;

                    _componentsService.SaveComponentConfig(_config);

                    //	RefreshTokenJob();
                    await Start();
                }
            }
        }
Exemple #3
0
        private async Task RefreshToken()
        {
            Logger.LogInformation("Refresh token");

            var res = await _httpClient.PostAsync(TokenAuthUrl,
                                                  HttpClientUtils.BuildFormParams(
                                                      new KeyValuePair <string, string>("grant_type", "refresh_token"),
                                                      new KeyValuePair <string, string>("refresh_token", _spotifyVault.AccessToken.RefreshToken),
                                                      new KeyValuePair <string, string>("client_id", Config.ClientSecret.ClientId),
                                                      new KeyValuePair <string, string>("client_secret", Config.ClientSecret.ClientSecret)));

            var st = await res.Content.ReadAsStringAsync();

            var newToken = st.FromJson <OAuthTokenResult>();

            _spotifyVault.AccessToken.AccessToken = newToken.AccessToken;
            _spotifyVault.AccessToken.ExpireOn    = DateTime.Now.AddSeconds(newToken.ExpiresIn);
            SaveVault(_spotifyVault);

            Logger.LogInformation($"Token refresh expire on: {_spotifyVault.AccessToken.ExpireOn}");

            InitSpotifyClient();
        }