public static bool GeoEnabled() { bool result = false; string url = "https://api.twitter.com/1.1/account/verify_credentials.json"; Dictionary <string, string> headers = TwitterOAuth.getInstance().createOAuthHeader("GET", url, new Dictionary <string, string>()); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ServicePoint.Expect100Continue = false; foreach (string key in headers.Keys) { request.Headers.Add(key, headers[key]); } request.Method = WebRequestMethods.Http.Get; request.Timeout = 8000; if (Properties.Settings.Default.ProxyServer.Length > 0) { request.Proxy = new WebProxy(string.Format("http://{0}:{1}", Properties.Settings.Default.ProxyServer, Properties.Settings.Default.ProxyPort)); } using (WebResponse response = request.GetResponse()) using (JsonTextReader reader = new JsonTextReader(new StreamReader(response.GetResponseStream()))) { /* * string r = reader.ReadToEnd(); * XmlDocument doc = new XmlDocument(); * doc.LoadXml(r); * XmlNodeList list = doc.SelectNodes("/user/geo_enabled"); * if (list != null && list.Count > 0) * { * XmlNode node = list[0]; * result = bool.Parse(node.InnerText); * } */ while (reader.Read()) { if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName && reader.Value.ToString() == "geo_enabled") { reader.Read(); result = (Boolean)reader.Value; } } } } return(result); }
private void btnXAuth_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; try { Properties.Settings.Default.OAuthToken = ""; Properties.Settings.Default.OAuthTokenSecret = ""; TwitterOAuth.getInstance().getAccessToken(txtTwitterID.Text, txtTwitterPassword.Text); MessageBox.Show("認証成功", "Twitter 認証", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "認証失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning); } Cursor.Current = Cursors.Default; }
public void OAuthGetFriendIds() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var ids = tweet.GetFriendIds(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET); Console.WriteLine(ids.ToJson()); Console.WriteLine(); foreach (long id in ids) { Console.WriteLine(id); } } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public void OAuthGetFollowers() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var response = tweet.GetFollowers(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET, "heatxsink", -1); Assert.GreaterOrEqual(response.Users.Length, 2); Console.WriteLine("TOTAL:{0}", response.Users.Length); foreach (User user in response.Users) { Console.WriteLine("{0}", user.ScreenName); Console.WriteLine("\t{0}", user.Following); Console.WriteLine(user.ToJson()); } Console.WriteLine(response.ToJson()); } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public static string Tweet(string status, GPSTrackPoint location) { string result; string url = "https://api.twitter.com/1.1/statuses/update.json"; Dictionary <string, string> postData = new Dictionary <string, string>(); //postData.Add("status", Util.URLEncode(status)); postData.Add("status", status); if (Properties.Settings.Default.LocationTwit) { postData.Add("lat", Math.Round(location.Latitude, 6).ToString()); postData.Add("long", Math.Round(location.Longitude, 6).ToString()); } Dictionary <string, string> headers = TwitterOAuth.getInstance().createOAuthHeader("POST", url, postData); StringBuilder sb = new StringBuilder(); bool first = true; foreach (string key in postData.Keys) { if (!first) { sb.Append("&"); } else { first = false; } sb.Append(String.Format("{0}={1}", key, Util.URLEncode(postData[key]))); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ServicePoint.Expect100Continue = false; request.ContentType = "application/x-www-form-urlencoded"; request.Method = WebRequestMethods.Http.Post; request.Timeout = 8000; foreach (string key in headers.Keys) { request.Headers.Add(key, headers[key]); } if (Properties.Settings.Default.ProxyServer.Length > 0) { request.Proxy = new WebProxy(string.Format("http://{0}:{1}", Properties.Settings.Default.ProxyServer, Properties.Settings.Default.ProxyPort)); } byte[] bytes = Encoding.ASCII.GetBytes(sb.ToString()); request.ContentLength = bytes.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); } using (WebResponse response = request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { result = reader.ReadToEnd(); } } return(result); }
public AuthenticateResult Authenticate(Server server, Connection connection, UserInfo userInfo) { // ニックネームとパスワードのチェック if (String.IsNullOrEmpty(userInfo.Nick)) { return(new AuthenticateResult(ErrorReply.ERR_NONICKNAMEGIVEN, "No nickname given")); } // OAuth ログイン/設定 if (String.IsNullOrEmpty(userInfo.Password)) { connection.SendGatewayServerMessage("* OAuth 認証の設定を開始します..."); return(new OAuthContinueAuthenticationResult()); } else { var config = Config.LoadConfig(userInfo.UserName); // HOSTING => 番号的 ID になる connection.SendGatewayServerMessage("* アカウント認証を確認しています(OAuth)..."); // OAuth 設定未設定 if (String.IsNullOrEmpty(config.OAuthAccessToken) || String.IsNullOrEmpty(config.OAuthTokenSecret)) { connection.SendGatewayServerMessage("* OAuth 認証の設定を開始します..."); return(new OAuthContinueAuthenticationResult()); } // 設定してあるパスワードとの照合 if (Utility.GetMesssageDigest(userInfo.Password) != config.OAuthUserPasswordHash) { connection.SendGatewayServerMessage("* アカウント認証に失敗しました。ユーザ名またはパスワードを確認してください。"); return(new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect")); } // ユーザー認証問い合わせをしてみる try { TwitterOAuth twitterOAuth = new TwitterOAuth(server.OAuthClientKey, server.OAuthSecretKey) { Token = config.OAuthAccessToken, TokenSecret = config.OAuthTokenSecret }; TwitterIdentity identity = new TwitterIdentity { Token = config.OAuthAccessToken, TokenSecret = config.OAuthTokenSecret }; TwitterService twitterService = new TwitterService(server.OAuthClientKey, server.OAuthSecretKey, identity); User twitterUser = twitterService.VerifyCredential(); identity.ScreenName = twitterUser.ScreenName; identity.UserId = twitterUser.Id; connection.SendGatewayServerMessage(String.Format("* アカウント: {0} (ID:{1})", twitterUser.ScreenName, twitterUser.Id)); return(new TwitterAuthenticateResult(twitterUser, identity)); } catch (Exception ex) { connection.SendServerErrorMessage(TwitterOAuth.GetMessageFromException(ex)); return(new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect")); } } }
public AuthenticateResult Authenticate(Server server, Connection connection, UserInfo userInfo) { // ニックネームとパスワードのチェック if (String.IsNullOrEmpty(userInfo.Nick)) { return new AuthenticateResult(ErrorReply.ERR_NONICKNAMEGIVEN, "No nickname given"); } // OAuth ログイン/設定 if (String.IsNullOrEmpty(userInfo.Password)) { connection.SendGatewayServerMessage("* OAuth 認証の設定を開始します..."); return new OAuthContinueAuthenticationResult(); } else { var config = Config.LoadConfig(userInfo.UserName); // HOSTING => 番号的 ID になる // xAuth fallback if (true && String.IsNullOrEmpty(config.OAuthUserPasswordHash)) { return new XAuthAuthentication().Authenticate(server, connection, userInfo); } connection.SendGatewayServerMessage("* アカウント認証を確認しています(OAuth)..."); // OAuth 設定未設定 if (String.IsNullOrEmpty(config.OAuthAccessToken) || String.IsNullOrEmpty(config.OAuthTokenSecret)) { connection.SendGatewayServerMessage("* OAuth 認証の設定を開始します..."); return new OAuthContinueAuthenticationResult(); } // 設定してあるパスワードとの照合 if (Utility.GetMesssageDigest(userInfo.Password) != config.OAuthUserPasswordHash) { connection.SendGatewayServerMessage("* アカウント認証に失敗しました。ユーザ名またはパスワードを確認してください。"); return new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect"); } // ユーザー認証問い合わせをしてみる try { TwitterOAuth twitterOAuth = new TwitterOAuth(server.OAuthClientKey, server.OAuthSecretKey) { Token = config.OAuthAccessToken, TokenSecret = config.OAuthTokenSecret }; TwitterIdentity identity = new TwitterIdentity { Token = config.OAuthAccessToken, TokenSecret = config.OAuthTokenSecret }; TwitterService twitterService = new TwitterService(server.OAuthClientKey, server.OAuthSecretKey, identity); User twitterUser = twitterService.VerifyCredential(); identity.ScreenName = twitterUser.ScreenName; identity.UserId = twitterUser.Id; connection.SendGatewayServerMessage(String.Format("* アカウント: {0} (ID:{1})", twitterUser.ScreenName, twitterUser.Id)); return new TwitterAuthenticateResult(twitterUser, identity); } catch (Exception ex) { connection.SendServerErrorMessage(TwitterOAuth.GetMessageFromException(ex)); return new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect"); } } }
public AuthenticateResult Authenticate(Server server, Connection connection, UserInfo userInfo) { // ニックネームとパスワードのチェック if (String.IsNullOrEmpty(userInfo.Nick)) { return(new AuthenticateResult(ErrorReply.ERR_NONICKNAMEGIVEN, "No nickname given")); } if (String.IsNullOrEmpty(userInfo.Password)) { return(new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect")); } // ログインチェック // この段階でTwitterServiceは作っておく connection.SendGatewayServerMessage("* アカウント認証を確認しています(xAuth)..."); User twitterUser; TwitterIdentity twitterIdentity; try { // xAuth // TODO: Monoの時だけ特別扱いする ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); }; TwitterOAuth twitterOAuth = new TwitterOAuth(server.OAuthClientKey, server.OAuthSecretKey); twitterIdentity = twitterOAuth.RequestAccessToken("", "", new Dictionary <string, string> { { "x_auth_mode", "client_auth" }, { "x_auth_username", userInfo.UserName }, { "x_auth_password", userInfo.Password } }); TwitterService twitter = new TwitterService(server.OAuthClientKey, server.OAuthSecretKey, twitterIdentity); twitterUser = twitter.VerifyCredential(); } catch (WebException we) { // Twitter の接続に失敗 connection.SendGatewayServerMessage("* アカウント認証に失敗しました。ユーザ名またはパスワードを確認してください。(" + TwitterOAuth.GetMessageFromException(we) + ")"); #if DEBUG foreach (var l in we.ToString().Split('\n')) { connection.SendGatewayServerMessage(l); } #endif return(new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect")); } catch (Exception ex) { // Twitter の接続に失敗 connection.SendGatewayServerMessage("* アカウント認証に失敗しました。ユーザ名またはパスワードを確認してください。内部的なエラーが発生しました。(" + ex.Message + ")"); Trace.TraceError(ex.ToString()); return(new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect")); } connection.SendGatewayServerMessage(String.Format("* アカウント: {0} (ID:{1})", twitterUser.ScreenName, twitterUser.Id)); return(new TwitterAuthenticateResult(twitterUser, twitterIdentity)); // 成功 }
protected void lnkbtnTwitterClick(object sender, EventArgs e) { TwitterOAuth.TwitterOpenAuth(); }
public void OAuthGetFriends() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var response = tweet.GetFriends(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET, TestConstants.TWITTER_USERNAME_READ, -1); Assert.AreEqual(2, response.Users.Length); Console.WriteLine("TOTAL: {0}", response.Users.Length); foreach (User user in response.Users) { Console.WriteLine("{0}", user.ScreenName); Console.WriteLine(user.ToJson()); } } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public void OAuthVerifyCredentials() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var user = tweet.VerifyCredentials(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET); Console.WriteLine(user.ToJson()); } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public void OAuthUpdateStatus() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var message = string.Format("Hello World, this is a test {0}.", DateTime.Now.Ticks.ToString()); var response = tweet.UpdateStatus(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET, message); Assert.AreEqual(message, response.Text); Console.WriteLine(response.ToJson()); } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public void OAuthShowUserLargeFriendCount() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var response = tweet.ShowUser(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET, "heatxsink"); Assert.AreEqual("heatxsink", response.ScreenName); Console.WriteLine(response.ToJson()); } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); } }
public void OAuthShowUser() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var response = tweet.ShowUser(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET, TestConstants.TWITTER_USERNAME_READ); Assert.AreEqual(TestConstants.TWITTER_USERNAME_READ, response.ScreenName); Console.WriteLine(response.ToJson()); } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public void OAuthSendDirectMessageWithScreeName() { TwitterOAuth tweet = new TwitterOAuth(TestConstants.TWITTER_CONSUMER_KEY, TestConstants.TWITTER_CONSUMER_SECRET); try { var text = "Hi, this is a direct message."; var response = tweet.SendDirectMessage(TestConstants.TWITTER_ACCESS_TOKEN, TestConstants.TWITTER_ACCESS_SECRET, TestConstants.TWITTER_USERNAME_READ, text); Assert.AreEqual(text, response.Text); Console.WriteLine("{0}", response.Text); Console.WriteLine(response.ToJson()); } catch (WebException ex) { Console.Error.WriteLine(Util.HandleWebException(ex)); Assert.Fail(); } }
public void OnClick() { TwitterOAuth twitterOAuth = new TwitterOAuth(this); twitterOAuth.LogOut(); }