コード例 #1
0
ファイル: TwitterClient.cs プロジェクト: Double222/Samurai
    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;
    }
コード例 #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 });
 }
コード例 #3
0
        static bool GetCredentials(out InMemoryCredentials credentials)
        {
            credentials = new InMemoryCredentials();

            if (File.Exists(CredentialsFile))
            {
                string[] lines = File.ReadAllLines(CredentialsFile);
                if (lines != null && lines.Length > 0)
                {
                    credentials.Load(lines[0]);
                    return true;
                }
            }

            // validate that credentials are present
            if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["twitterConsumerKey"]) ||
                string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["twitterConsumerSecret"]))
            {
                MessageBox.Show("\nCan't Run Yet\n" +
                                  "-------------\n" +
                                  "You need to set twitterConsumerKey and twitterConsumerSecret \n" +
                                  "in App.config/appSettings.\nPlease visit http://dev.twitter.com/apps for more info.\n");
                return false;
            }

            credentials = new InMemoryCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
            };

            return true;
        }
コード例 #4
0
ファイル: SignIn.aspx.cs プロジェクト: prog-moh/LinqToTwitter
    protected void Page_Load(object sender, EventArgs e)
    {
        IOAuthCredentials credentials = new InMemoryCredentials();
        string authString = Session[OAuthCredentialsKey] as string;

        if (authString == null)
        {
            credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];

            Session[OAuthCredentialsKey] = credentials.ToString();
        }
        else
        {
            credentials.Load(authString);
        }

        auth = new SignInAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
            },
            PerformRedirect = authUrl => Response.Redirect(authUrl)
        };

        if (!Page.IsPostBack)
        {
            if (!string.IsNullOrWhiteSpace(credentials.ConsumerKey) &&
                !string.IsNullOrWhiteSpace(credentials.ConsumerSecret))
            {
                AuthMultiView.ActiveViewIndex = 1;

                if (auth.CompleteAuthorization(Request.Url))
                {
                    AuthMultiView.SetActiveView(SignedInView);
                    screenNameLabel.Text = auth.ScreenName;
                }
            }
        }
    }
コード例 #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());
        }
コード例 #6
0
    protected void Page_Load(object sender, EventArgs e)
    {
        IOAuthCredentials credentials = new InMemoryCredentials();
        string authString = Session[OAuthCredentialsKey] as string;

        if (authString == null)
        {
            credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];

            Session[OAuthCredentialsKey] = credentials.ToString();
        }
        else
        {
            credentials.Load(authString);
        }

        auth = new WebAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
            },
            PerformRedirect = authUrl => Response.Redirect(authUrl)
        };

        if (string.IsNullOrEmpty(credentials.ConsumerKey) ||
            string.IsNullOrEmpty(credentials.ConsumerSecret) ||
            !auth.IsAuthorized)
        {
            // Authorization occurs only on the home page.
            Response.Redirect("~/");
        }

        updateBox.Focus();
    }
コード例 #7
0
ファイル: TwitterService.cs プロジェクト: Dakuan/RiftData
        public TwitterService()
        {
            var credentials = new InMemoryCredentials { AccessToken = AccessTokenSecret, ConsumerKey = ConsumerKey, ConsumerSecret = ConsumerSecret, OAuthToken = AccessToken };

            this.authorizer = new SingleUserAuthorizer { Credentials = credentials };
        }
コード例 #8
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();
                }
            }
        }