public static void Main(string[] args) { ConsoleOutput.PrintMessage("BoxKite.Twitter Live Fire Tests (Initiate User Auth)"); ConsoleOutput.PrintMessage("(control-c ends at anytime)"); var twitterConnection = new TwitterConnection("3izxqWiej34yTlofisw", "uncicYQtDx5SoWth1I9xcn5vrpczUct1Oz9ydwTY4"); var authString = twitterConnection.BeginUserAuthentication().Result; // if the response is null, something is wrong with the initial request to OAuth if (!string.IsNullOrWhiteSpace(authString)) { ConsoleOutput.PrintMessage("Pin: "); var pin = System.Console.ReadLine(); twitterCredentials = twitterConnection.CompleteUserAuthentication(pin, authString).Result; } if (twitterCredentials.Valid) { ConsoleOutput.PrintMessage(twitterCredentials.ScreenName + " is authorised to use BoxKite.Twitter."); } else { ConsoleOutput.PrintMessage("Something Went Wrong during User Authentication Dance."); } ConsoleOutput.PrintMessage("Press Return to close window"); System.Console.ReadLine(); }
private static async Task MainAsync(string[] args) { var twitterCredentials = new TwitterCredentials() { ConsumerKey = AppSettings.ApplicationConsumerKey, ConsumerSecret = AppSettings.ApplicationConsumerSecret, }; var twitter = new UserSession(twitterCredentials, new DesktopPlatformAdaptor()); var oathToken = await twitter.StartUserAuthentication(); Console.Write("Please enter the PIN as displayed in the new browser window: "); var pin = Console.ReadLine(); var credentials = await twitter.ConfirmPin(pin, oathToken); if (credentials.Valid) { const string filename = "credentials.json"; string json = JsonConvert.SerializeObject(credentials); File.WriteAllText(filename, json); Console.WriteLine("Credentials were successfully saved to '{0}'", filename); } else { Console.WriteLine("Credentials were invalid."); } Console.ReadLine(); }
public static void SaveTwitterCredentialsToFile(TwitterCredentials twitterCreds) { var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), BoxKiteTwitterCredentialsStore); if (File.Exists(fileName)) { File.Delete(fileName); } SaveCredentialsToFile(fileName, twitterCreds); }
public static TwitterCredentials GetTwitterCredentialsFromFile() { var twitterCreds = new TwitterCredentials(); var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),BoxKiteTwitterCredentialsStore); if (File.Exists(fileName)) { twitterCreds = GetCredentialsFromFile(fileName); } return twitterCreds.Valid ? twitterCreds : null; }
public UserSession(TwitterCredentials credentials, IPlatformAdaptor platformAdaptor, int _waitTimeoutSeconds = 30) : base(credentials.ConsumerKey, credentials.ConsumerSecret, _waitTimeoutSeconds) { TwitterCredentials = credentials; clientID = credentials.ConsumerKey; clientSecret = credentials.ConsumerSecret; bearerToken = credentials.BearerToken; PlatformAdaptor = platformAdaptor; waitTimeoutSeconds = _waitTimeoutSeconds; IsActive = true; }
public TwitterConnection(TwitterCredentials twitterCredentials) { PlatformAdaptor = new DesktopPlatformAdaptor(); TwitterCredentials = twitterCredentials; UserSession = BuildUserSession(); }
public TwitterConnection(TwitterCredentials twitterCredentials, IPlatformAdaptor platformAdaptor = null) { PlatformAdaptor = platformAdaptor; TwitterCredentials = twitterCredentials; UserSession = BuildUserSession(); }
private static void SaveCredentialsToFile(string fileName, TwitterCredentials tc) { var json = JsonConvert.SerializeObject(tc, Formatting.Indented); var cypherText = json.ToSecureString().EncryptString(); File.WriteAllText(fileName, cypherText); }
private static void GetTweets(TwitterCredentials twittercredentials) { if (twittercredentials != null) { TwitterConnection = new TwitterConnection(twittercredentials); TwitterConnection.StartUserStreaming(); ConsoleOutput.PrintMessage(TwitterConnection.TwitterCredentials.ScreenName +" is authorised to use BoxKite.Twitter."); var usersession = TwitterConnection.UserSession; var userstream = TwitterConnection.UserStream; var applicationsession = TwitterConnection.ApplicationSession; new List<Tweet>(); var searchstream = usersession.StartSearchStream(track: "Intel"); searchstream.FoundTweets.Subscribe(ProcessTweet); searchstream.Start(); Thread.Sleep(TimeSpan.FromMinutes(120)); searchstream.CancelStream.Cancel(); searchstream.Stop(); } }
public static async Task<TwitterCredentials> ConfirmPin(this IUserSession session, string pinAuthorizationCode, string oAuthToken) { if (string.IsNullOrWhiteSpace(pinAuthorizationCode)) throw new ArgumentException("pin AuthorizationCode must be specified", pinAuthorizationCode); var sinceEpoch = session.GenerateTimestamp(); var nonce = session.GenerateNoonce(); var dataToPost = string.Format( "OAuth realm=\"\", oauth_nonce=\"{0}\", oauth_timestamp=\"{1}\", oauth_consumer_key=\"{2}\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_verifier=\"{3}\", oauth_token=\"{4}\"", nonce, sinceEpoch, session.clientID, pinAuthorizationCode, oAuthToken); var response = await PostData(TwitterApi.AuthorizeTokenUrl(), dataToPost); if (string.IsNullOrWhiteSpace(response)) return TwitterCredentials.Null; //oops something wrong here var accessToken = ""; var accessTokenSecret = ""; var userId = ""; var screenName = ""; foreach (var splits in response.Split('&').Select(t => t.Split('='))) { switch (splits[0]) { case "oauth_token": //these tokens are request tokens, first step before getting access tokens accessToken = splits[1]; break; case "oauth_token_secret": accessTokenSecret = splits[1]; break; case "user_id": userId = splits[1]; break; case "screen_name": screenName = splits[1]; break; } } if (accessToken != null && accessTokenSecret != null && userId != null && screenName != null) { if (await session.StartApplicationOnlyAuth()) { var twitterCreds = new TwitterCredentials() { ConsumerKey = session.clientID, ConsumerSecret = session.clientSecret, BearerToken = session.bearerToken, ScreenName = screenName, Token = accessToken, TokenSecret = accessTokenSecret, UserID = Int64.Parse(userId), Valid = true }; session.IsActive = true; return twitterCreds; } } return TwitterCredentials.Null; }