Esempio n. 1
0
        private MvcAuthorizer GetAuthorizer(string twitterUserId)
        {
            var twitterKey    = ConfigurationManager.AppSettings["TwitterConsumerKey"];
            var twitterSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"];

            IOAuthCredentials credentials = new InMemoryCredentials();

            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey    = twitterKey;
                credentials.ConsumerSecret = twitterSecret;
            }

            if (!string.IsNullOrEmpty(twitterUserId))
            {
                var challengerModel = _repository.Get(twitterUserId);
                credentials.AccessToken =
                    challengerModel.Connections.Single(x => x.ConnectionName == "Twitter").AccessToken;
                credentials.OAuthToken =
                    challengerModel.Connections.Single(x => x.ConnectionName == "Twitter").OAuthToken;
            }


            var auth = new MvcAuthorizer
            {
                Credentials = credentials
            };

            return(auth);
        }
Esempio n. 2
0
        //
        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            //return View();
            if (!Request.IsAuthenticated)
            {
                var credentials = new InMemoryCredentials();
                credentials.ConsumerKey    = ConfigurationManager.AppSettings["TwitterCustomerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["TwitterCustomerSecret"];
                var auth = new MvcAuthorizer {
                    Credentials = credentials
                };
                auth.CompleteAuthorization(Request.Url);
                if (!auth.IsAuthorized)
                {
                    return(auth.BeginAuthorization(Request.Url));
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(auth.ScreenName, true);
                    PostworthyUser pm = UsersCollection.Single(auth.ScreenName, addIfNotFound: true);
                    if (string.IsNullOrEmpty(pm.AccessToken) && string.IsNullOrEmpty(pm.OAuthToken))
                    {
                        pm.AccessToken = auth.Credentials.AccessToken;
                        pm.OAuthToken  = auth.Credentials.OAuthToken;
                        UsersCollection.Save();
                    }
                    return(RedirectToAction("Index", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
                }
            }
            else
            {
                return(RedirectToAction("Index", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
            }
        }
Esempio n. 3
0
        public ITwitterAuthorizer Auth()
        {
            var credentials = new InMemoryCredentials
            {
                ConsumerKey    = this.consumerKey,
                ConsumerSecret = this.consumerSecret
            };

            if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(oAuthToken))
            {
                credentials.AccessToken = this.accessToken;
                credentials.OAuthToken  = this.oAuthToken;
            }

            var auth = new PinAuthorizer
            {
                Credentials              = credentials,
                UseCompression           = true,
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                {
                    Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n");
                    Console.Write("Enter the PIN number here: ");
                    return(Console.ReadLine());
                }
            };

            auth.Authorize();

            return(auth);
        }
Esempio n. 4
0
        public void Connect()
        {
            IOAuthCredentials credentials = new InMemoryCredentials()
            {
                ConsumerKey    = Secrets.ApiKeys.TwitterConsumerKey,
                ConsumerSecret = Secrets.ApiKeys.TwitterConsumerSecret,
                OAuthToken     = Secrets.ApiKeys.TwitterOAuthToken,
                AccessToken    = Secrets.ApiKeys.TwitterAccesstoken
            };

            _twitterCtx = new TwitterContext(new SingleUserAuthorizer()
            {
                Credentials = credentials
            });
        }
Esempio n. 5
0
        public void StreamUsersTweets(IList <RetreaveIndex> indexesToStream)
        {
            Running = true;
            DoUserStreams(indexesToStream);

            return;

            StringBuilder usersToFollow = new StringBuilder();
            bool          first         = true;

            //foreach uesr get the associated users
            //should only be one anyway.
            foreach (RetreaveIndex index in indexesToStream)
            {
                foreach (RegisteredUser user in index.AssociatedUsers)
                {
                    if (!first)
                    {
                        usersToFollow.Append(",");
                    }

                    usersToFollow.Append(user.TwitterId);
                    first = false;
                }
            }

            Console.WriteLine("Starting stream for " + usersToFollow.ToString());
            InMemoryCredentials credentials = new InMemoryCredentials();

            //user credentials
            credentials.AccessToken = AuthenticationTokens.AppOwnerAccessTokenSecret;
            credentials.OAuthToken  = AuthenticationTokens.AppOwnerAccessToken;

            //app specific credentials
            credentials.ConsumerKey    = AuthenticationTokens.TwitterConsumerKey;
            credentials.ConsumerSecret = AuthenticationTokens.TwitterConsumerSecret;

            //save to pin authorizer
            PinAuthorizer authorizer = new PinAuthorizer();

            authorizer.Credentials = credentials;


            TwitterContext twitterCtx = new TwitterContext(authorizer);

            //DoSiteStream(twitterCtx, usersToFollow.ToString());
        }
Esempio n. 6
0
        private void DoUserStreams(IList <RetreaveIndex> indexesToStream)
        {
            foreach (RetreaveIndex index in indexesToStream)
            {
                foreach (RegisteredUser user in index.AssociatedUsers)
                {
                    Console.WriteLine("Starting stream for " + user.UserName);
                    InMemoryCredentials credentials = new InMemoryCredentials();
                    //user credentials
                    credentials.AccessToken = user.AuthDetails.AccessTokenSecret;
                    credentials.OAuthToken  = user.AuthDetails.AccessToken;

                    //app specific credentials
                    credentials.ConsumerKey    = AuthenticationTokens.TwitterConsumerKey;
                    credentials.ConsumerSecret = AuthenticationTokens.TwitterConsumerSecret;

                    //save to pin authorizer
                    PinAuthorizer authorizer = new PinAuthorizer();
                    authorizer.Credentials = credentials;


                    TwitterContext twitterCtx = new TwitterContext(authorizer);

                    var streaming =
                        (from strm in twitterCtx.UserStream
                         where strm.Type == UserStreamType.User
                         select strm)
                        .StreamingCallback(strm =>
                    {
                        StringBuilder blockBuilder = new StringBuilder();
                        int bracketCount           = 0;
                        for (int i = 0; i < strm.Content.Length; i++)
                        {
                            blockBuilder.Append(strm.Content[i]);

                            if (!new[] { '{', '}' }.Contains(strm.Content[i]))
                            {
                                continue;
                            }

                            if (strm.Content[i] == '{')
                            {
                                bracketCount++;
                            }

                            if (strm.Content[i] == '}')
                            {
                                bracketCount--;
                            }

                            if (bracketCount == 0)
                            {
                                //TODO: remove the index identifier when site streaming turned on
                                Action <string, string> parseMethod = ParseMessage;
                                parseMethod.BeginInvoke(blockBuilder.ToString().Trim('\n'), index.IndexStreamIdentifier,
                                                        null, null);
                                blockBuilder.Clear();
                            }
                        }

                        if (!Running)
                        {
                            strm.CloseStream();
                        }
                    })
                        .SingleOrDefault();
                }
            }
        }