Esempio n. 1
0
        public override void HandleNavigation(object args)
        {
            // Identify the app
            AppCredentials = new TwitterCredentials(_consumerKey, _consumerSecret);

            // The user already was authorized
            if (SavedToken.IsSet)
            {
                // Load saved token from file
                Token token = SavedToken.Load();

                // Set up the application credentials
                TwitterCredentials credentials = new TwitterCredentials(AppCredentials.ConsumerKey,
                                                                        AppCredentials.ConsumerSecret, token.AccessToken, token.AccessTokenSecret);
                Tweetinvi.Auth.SetCredentials(credentials);

                if (Tweetinvi.User.GetLoggedUser() != null)
                {
                    var mainController = new MainController();
                    mainController.HandleNavigation(null);
                }
                else
                {
                    Window.ShowError("Connection problem.");
                    Window.Show();
                }
            }
            else
            {
                // The user need to log in
                Window.Show();
            }
        }
Esempio n. 2
0
        private void Window_btnLogOutClicked(object sender, EventArgs e)
        {
            // Unset credentials
            Tweetinvi.Auth.SetCredentials(null);
            // Delete saved token
            SavedToken.Delete();

            Window.Close();
            // Show LoginWindow
            new LoginController {
                Window = new LoginWindow()
            }.HandleNavigation(null);
        }
Esempio n. 3
0
        private void Window_btnLoginClicked(object sender, EventArgs e)
        {
            try
            {
                // With tthe pin code it is now possible to get the credentials back from Twitter
                ITwitterCredentials credentials = CredentialsCreator.GetCredentialsFromVerifierCode
                                                      (Window.Pin, AppCredentials);

                if (credentials != null)
                {
                    // Use the user credentials
                    Tweetinvi.Auth.SetCredentials(credentials);
                }

                if (Tweetinvi.User.GetLoggedUser() != null)
                {
                    var mainController = new MainController();
                    mainController.HandleNavigation(null);

                    if (Window.RememberChecked)
                    {
                        // Save user credentials
                        SavedToken.Set(new Token(Tweetinvi.User.GetLoggedUser().Name,
                                                 credentials.AccessToken, credentials.AccessTokenSecret));
                    }

                    Window.Close();
                }
                else
                {
                    Window.ShowError("Incorrect PIN.");
                    AppCredentials.AuthorizationKey    = null;
                    AppCredentials.AuthorizationSecret = null;
                    Window.RepeatAuthorization();
                }
            }
            catch (Exception)
            {
                new ErrorDialog("Cannot login. Check your internet connection and entered PIN-code");
            }
        }
Esempio n. 4
0
 private void sendToken(IOAuth2Token tok)
 {
     SavedToken?.Invoke(null, tok);
 }
Esempio n. 5
0
        private async Task <Token> TokenGetter()
        {
            /*the ToKen gets serialized to json and saved for later used, the same token can be used to get a SpotifyWebAPI connection
             * but there's a time limitation. and we check if the time hasn't passed we use that token
             * if it has we just get another one*/

            try
            {
                SavedToken savedToken;
                Token      token;
                if (System.IO.File.Exists(MyResources.Instance.TokenPath))
                {
                    using (var sr = new StreamReader(MyResources.Instance.TokenPath, Encoding.UTF8))
                    {
                        savedToken = JsonConvert.DeserializeObject <SavedToken>(await sr.ReadToEndAsync());
                    }
                    if (savedToken.ExpiresInMinutes < 5)
                    {
                        token = GetToken();
                        if (token.HasError())
                        {
                            throw new Exception(token.ErrorDescription);
                        }
                        using (var sw = new StreamWriter(MyResources.Instance.TokenPath, false, Encoding.UTF8))
                        {
                            savedToken = new SavedToken
                            {
                                TokenType   = token.TokenType,
                                AccessToken = token.AccessToken,
                                ExpiresIn   = token.ExpiresIn,
                                CreateDate  = DateTime.Now
                            };
                            await sw.WriteAsync(JsonConvert.SerializeObject(savedToken, Formatting.Indented));
                        }
                    }
                    else
                    {
                        token = new Token
                        {
                            TokenType   = savedToken.TokenType,
                            AccessToken = savedToken.AccessToken,
                            ExpiresIn   = savedToken.ExpiresIn,
                            CreateDate  = savedToken.CreateDate
                        };
                    }
                }
                else
                {
                    token = GetToken();
                    if (token.HasError())
                    {
                        throw new Exception(token.ErrorDescription);
                    }
                    using (var sw = new StreamWriter(MyResources.Instance.TokenPath, false, Encoding.UTF8))
                    {
                        savedToken = new SavedToken
                        {
                            TokenType   = token.TokenType,
                            AccessToken = token.AccessToken,
                            ExpiresIn   = token.ExpiresIn,
                            CreateDate  = DateTime.Now
                        };
                        await sw.WriteAsync(JsonConvert.SerializeObject(savedToken, Formatting.Indented));
                    }
                }
                return(token);
            }
            catch (Exception e)
            {
                if (System.IO.File.Exists(MyResources.Instance.TokenPath))
                {
                    System.IO.File.Delete(MyResources.Instance.TokenPath);
                }

                throw;
            }
        }