Skip to content

Commit

Permalink
Fix OAuth refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
philippn committed Feb 4, 2019
1 parent c10f10c commit bc05dd4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
9 changes: 5 additions & 4 deletions NowPlayingKiosk/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public partial class MainWindow : Window
{
private bool _inStateChange;
private TrackInfoPoller poller;
private SpotifyWebAPI webApi;

public MainWindow()
{
Expand Down Expand Up @@ -218,19 +219,19 @@ private async void LoggedInSuccessfully(object sender, AuthorizationCode payload
Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => GoToMainPage()));

Token token = await auth.ExchangeCode(payload.Code);
SpotifyWebAPI api = new SpotifyWebAPI
webApi = new SpotifyWebAPI
{
AccessToken = token.AccessToken,
TokenType = token.TokenType
};

StartPoller(api);
StartPoller(auth, token);
}

private void StartPoller(SpotifyWebAPI api)
private void StartPoller(AuthorizationCodeAuth auth, Token token)
{
CoverManager coverManager = new CoverManager();
NowPlayingTrackInfoProvider trackInfoProvider = new SpotifyNowPlayingTrackInfoProvider(api);
NowPlayingTrackInfoProvider trackInfoProvider = new SpotifyNowPlayingTrackInfoProvider(webApi, auth, token);
poller = new TrackInfoPoller(trackInfoProvider, coverManager, this);
Thread pollerThread = new Thread(new ThreadStart(poller.PollAndUpdate));
pollerThread.Start();
Expand Down
22 changes: 18 additions & 4 deletions NowPlayingKiosk/SpotifyNowPlayingTrackInfoProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
Expand All @@ -7,16 +8,29 @@ namespace NowPlayingKiosk
{
public class SpotifyNowPlayingTrackInfoProvider : NowPlayingTrackInfoProvider
{
private readonly SpotifyWebAPI clientApi;
private readonly SpotifyWebAPI webApi;
private readonly AuthorizationCodeAuth authorization;
private Token accessToken;
private Token refreshToken;

public SpotifyNowPlayingTrackInfoProvider(SpotifyWebAPI api)
public SpotifyNowPlayingTrackInfoProvider(SpotifyWebAPI api, AuthorizationCodeAuth auth, Token token)
{
clientApi = api;
webApi = api;
authorization = auth;
accessToken = token;
refreshToken = token;
}

public TrackInfo WhatIsNowPlaying()
{
PlaybackContext context = clientApi.GetPlayingTrack();
DateTime expireDate = accessToken.CreateDate.AddSeconds(accessToken.ExpiresIn);
if (DateTime.Compare(DateTime.Now, expireDate.AddMinutes(-1)) > 0)
{
accessToken = authorization.RefreshToken(refreshToken.RefreshToken).Result;
webApi.AccessToken = accessToken.AccessToken;
}

PlaybackContext context = webApi.GetPlayingTrack();

if (context.IsPlaying && !TrackType.Ad.Equals(context.CurrentlyPlayingType))
{
Expand Down

0 comments on commit bc05dd4

Please sign in to comment.