Skip to content

Commit

Permalink
token check should work now #23
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPNS committed Jun 3, 2020
1 parent e3f1381 commit 30a989b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Release/version.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.0.1
1.4.0.2
4 changes: 2 additions & 2 deletions Spotify Stream Widget/My Project/AssemblyInfo.vb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("1.4.0.1")>
<Assembly: AssemblyFileVersion("1.4.0.1")>
<Assembly: AssemblyVersion("1.4.0.2")>
<Assembly: AssemblyFileVersion("1.4.0.2")>
17 changes: 14 additions & 3 deletions Spotify Stream Widget/Viewer.vb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

Imports SpotifyAPI.Web, SpotifyAPI.Web.Auth, SpotifyAPI.Web.Enums, SpotifyAPI.Web.Models
Imports Spotify_Stream_Widget.Logger
Imports Unosquare.Swan

Public Class Viewer
Private Shared _spotify As SpotifyWebAPI
Private Shared _spotifyAuth As TokenSwapAuth = New TokenSwapAuth("https://spotify-token-swap.camefrom.space/", "http://localhost:4002", Scope.UserReadPlaybackState)
Expand Down Expand Up @@ -168,6 +170,7 @@ Public Class Viewer
'Spotify Auth
MsgBox("The Widget will open your browser to connect to the Spotify API")
AddHandler _spotifyAuth.AuthReceived, AddressOf _spotify_AuthReceived
AddHandler _spotifyAuth.OnAccessTokenExpired, AddressOf _spotify_OnAccessTokenExpired
_spotifyAuth.Start()
_spotifyAuth.OpenBrowser()
UpdateTrack()
Expand All @@ -186,6 +189,14 @@ Public Class Viewer
_spotifyAuth.Stop()
End Sub

Private Async Sub _spotify_OnAccessTokenExpired(sender, e)
Dim _newToken = Await _spotifyAuth.RefreshAuthAsync(_previousToken.RefreshToken)
_spotify.AccessToken = _newToken.AccessToken
_previousToken.AccessToken = _newToken.AccessToken
_previousToken.CreateDate = _newToken.CreateDate
Log("Auth refreshed: " & _spotify.AccessToken)
End Sub

Private Async Sub UpdateTrack()

'Kill it if the Viewer is closed.
Expand All @@ -203,13 +214,13 @@ Public Class Viewer
'could fail if you do not have a internet connection
Try
'check if new token is needed


If _previousToken.IsExpired() Then
Log("Token expired")
'We have a Event that handle the swap. This is just a notification.
Settings.StatusLabel.Text = "Status: Connection lost. Try to refresh..."
Settings.StatusLabel.ForeColor = Color.Yellow
_spotify.AccessToken = (Await _spotifyAuth.RefreshAuthAsync(_previousToken.RefreshToken)).AccessToken
Log("Auth refreshed")
_spotify_OnAccessTokenExpired("", "")
Await Task.Delay(5000)
UpdateTrack()
Return
Expand Down

0 comments on commit 30a989b

Please sign in to comment.