Beispiel #1
0
        //
        // GET: /Twitter/
        public ActionResult Authorize()
        {
            // OAuth Access Token Exchange
            TwitterService service = new TwitterService("consumerKey", "consumerSecret");
            OAuthAccessToken access = service.GetAccessTokenWithXAuth("username", "password"); //

            return View();
        }
Beispiel #2
0
        public Account Authenticate(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName) || String.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("userName");
            }

            TwitterService service = new TwitterService(ConfigurationManager.AppSettings["ConsumerKey"],
                ConfigurationManager.AppSettings["ConsumerSecret"]);

            OAuthAccessToken access = service.GetAccessTokenWithXAuth(userName, password);

            service.AuthenticateWith(access.Token, access.TokenSecret);

            var profile = service.GetUserProfile();

            Account account = AccountManager.Instance.GetCurrentAccounts().Where(acc => acc.Username == profile.ScreenName).FirstOrDefault();
            if (account != null)
            {
                throw new AuthFailureException("User " + account.Username + " already has an account with TweetOBox.");
            }
            if (profile != null && account == null)
            {
                account = new Account();
                account.Username = profile.ScreenName;
                // account.Password = profile.p
                account.AccountType = (int)AccountTypeEnum.Twitter;
                account.AccessToken = access.Token;
                account.AccessTokenSecret = access.TokenSecret;
                account.IsOAuth = true;
                AccountManager.Instance.AddAccount(account, false);
            }
            else
            {
                throw new AuthFailureException(service.Response.StatusDescription);
            }

            return account;
        }
        /// <summary>
        /// uses the tweetsharp api to get all tweets for the given company
        /// </summary>
        /// <param name="companyname">the name of the company</param>
        /// <returns>all tweets formated for the input string for the sentiment analysis</returns>
        private string getTweets(string companyname)
        {
            Console.WriteLine("Getting the tweets...");
            TwitterService twitterService = new TwitterService("H70mkcnY9uKDDGUcKywlBA", "fiobr6kG9OKwNHIU5D18dbpxWE5KdxWD8GRPRhMVII");
            OAuthAccessToken access = twitterService.GetAccessTokenWithXAuth("2012AIC", "!aicgroup");

            ArrayList pages = new ArrayList();
            JsonSerializer serializer = new JsonSerializer();

            // you can changes this number to get more tweets (100 tweets per page)
            int numPages = 1;

            // get the tweets from the first x numpages
            for (int i = 1; i <= numPages; i++)
            {
                TwitterSearchResult response = twitterService.Search(companyname, i, 100);
                RootObject page = (RootObject)serializer.Deserialize(new JsonTextReader(new StringReader(response.RawSource)), typeof(RootObject));
                pages.Add(page);
            }

            string result = "{'data': [";

            int count = 0;
            int eng = 0;

            foreach (RootObject curPage in pages)
            {
                foreach (var tweet in curPage.results)
                {
                    count++;
                    if (tweet.iso_language_code.Equals("en"))
                    {
                        eng++;
                        string tweettext = tweet.text;
                        tweettext = replaceChars(tweettext);

                        result += "{'text': '" + tweettext + "', 'query': '" + companyname + "'}, ";
                    }
                }
            }

            Console.WriteLine(count + " tweets fetched, " + eng + " were english and saved for the sentiment analysis.\n");

            // get rid of the last comma
            result = result.Substring(0, result.Length - 2);
            result += "]}";
            return result;
        }
		private static void TestWithTweetSharpXAuth()
		{
			// OAuth Access Token Exchange
			TwitterService twitterService = new TwitterService(OAuthProperties.ConsumerKey, OAuthProperties.ConsumerKeySecret);
			twitterService.AuthenticateWith(OAuthProperties.AccessToken, OAuthProperties.AccessTokenSecret);

			Console.WriteLine("Enter Username...");
			string username = Console.ReadLine();
			Console.WriteLine("Enter Password...");
			string password = Console.ReadLine();
			OAuthAccessToken accessToken = twitterService.GetAccessTokenWithXAuth(username, password);

			twitterService.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
			var verifyCredentialsOptions = new VerifyCredentialsOptions { IncludeEntities = true };
			TwitterUser user = twitterService.VerifyCredentials(verifyCredentialsOptions);
		}