public async Task ConnectWebApi(bool keepRefreshToken = true) { _securityStore = SecurityStore.Load(pluginDirectory); AuthorizationCodeAuth auth = new AuthorizationCodeAuth(_securityStore.ClientId, _securityStore.ClientSecret, "http://localhost:4002", "http://localhost:4002", Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative | Scope.UserReadCurrentlyPlaying | Scope.UserReadPlaybackState | Scope.UserModifyPlaybackState | Scope.Streaming | Scope.UserFollowModify); if (_securityStore.HasRefreshToken && keepRefreshToken) { Token token = await auth.RefreshToken(_securityStore.RefreshToken); _spotifyApi = new SpotifyWebAPI() { TokenType = token.TokenType, AccessToken = token.AccessToken }; } else { auth.AuthReceived += async(sender, payload) => { auth.Stop(); Token token = await auth.ExchangeCode(payload.Code); _securityStore.RefreshToken = token.RefreshToken; _securityStore.Save(pluginDirectory); _spotifyApi = new SpotifyWebAPI() { TokenType = token.TokenType, AccessToken = token.AccessToken }; }; auth.Start(); auth.OpenBrowser(); } }
public async Task ConnectWebClient(bool keepRefreshToken = true) { _securityStore = SecurityStore.Load(pluginDirectory); EmbedIOAuthServer _server = new EmbedIOAuthServer(new Uri("http://localhost:4002/callback"), 4002); if (_securityStore.HasRefreshToken && keepRefreshToken) { var refreshRequest = new AuthorizationCodeRefreshRequest(_securityStore.ClientId, _securityStore.ClientSecret, _securityStore.RefreshToken); var refreshResponse = await new OAuthClient().RequestToken(refreshRequest); lock (_lock) { _spotifyClient = new SpotifyClient(refreshResponse.AccessToken); } } else { await _server.Start(); _server.AuthorizationCodeReceived += async(object sender, AuthorizationCodeResponse response) => { await _server.Stop(); AuthorizationCodeTokenResponse token = await new OAuthClient().RequestToken( new AuthorizationCodeTokenRequest(_securityStore.ClientId, _securityStore.ClientSecret, response.Code, _server.BaseUri)); lock (_lock) { _securityStore.RefreshToken = token.RefreshToken; _securityStore.Save(pluginDirectory); _spotifyClient = new SpotifyClient(token.AccessToken); } }; _server.ErrorReceived += async(object sender, string error, string state) => { Console.WriteLine($"Aborting authorization, error received: {error}"); await _server.Stop(); }; var request = new LoginRequest(_server.BaseUri, _securityStore.ClientId, LoginRequest.ResponseType.Code) { Scope = new List <string> { UserLibraryRead, UserReadEmail, UserReadPrivate, UserReadPlaybackPosition, UserReadCurrentlyPlaying, UserReadPlaybackState, UserModifyPlaybackState, AppRemoteControl, PlaylistReadPrivate } }; Uri uri = request.ToUri(); try { BrowserUtil.Open(uri); } catch (Exception) { Console.WriteLine("Unable to open URL, manually open: {0}", uri); }; }; }