void GetInfo() { SetContentView(Resource.Layout.EditText); Button btn = FindViewById <Button> (Resource.Id.SumbitShare); btn.Click += delegate { var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; var filePath = System.IO.Path.Combine(sdCardPath, "test.png"); ThreadPool.QueueUserWorkItem(state => { try { EditText context = FindViewById <EditText>(Resource.Id.TextShare); string textShare = context.Text; var f = new Java.IO.File(filePath); StatusUpdate updStatus = new StatusUpdate(textShare); updStatus.SetMedia(f); twitter.UpdateStatus(updStatus); } catch (Java.Lang.Exception ex) { Console.WriteLine(ex); } }); }; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById <Button>(Resource.Id.MyButton); // TODO change OAuth keys and tokens and username and password ConfigurationBuilder config = new ConfigurationBuilder(); config.SetOAuthConsumerKey("YourConsumerKey"); config.SetOAuthConsumerSecret("YourConsumerSecret"); config.SetOAuthAccessToken("YourAccessToken"); config.SetOAuthAccessTokenSecret("YourAccessTokenSecret"); config.SetUser("YouUsername"); config.SetPassword("YourPassword"); TwitterFactory factory = new TwitterFactory(config.Build()); ITwitter twitter = factory.Instance; button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); ThreadPool.QueueUserWorkItem(state => { try { twitter.UpdateStatus("This is a test"); } catch (Java.Lang.Exception ex) { Console.WriteLine(ex); } }); }; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Assigning controls Button button = FindViewById <Button> (Resource.Id.myButton); EditText username = FindViewById <EditText> (Resource.Id.editText1); EditText password = FindViewById <EditText> (Resource.Id.editText2); // Twitter configuration part ConfigurationBuilder TwitterConfig = new ConfigurationBuilder(); TwitterConfig.SetOAuthConsumerKey("_replace_wit_your_app_consumer_key_"); TwitterConfig.SetOAuthConsumerSecret("_replace_wit_your_app_consumer_secret_"); IConfiguration twiConfigInterface = TwitterConfig.Build(); TwitterFactory factory = new TwitterFactory(twiConfigInterface); OAuthAuthorization oath = new OAuthAuthorization(twiConfigInterface); AccessToken ascTkn = oath.GetOAuthAccessToken(username.Text, password.Text); ITwitter twitter = factory.GetInstance(ascTkn); button.Click += delegate { ThreadPool.QueueUserWorkItem(state => { try { twitter.UpdateStatus("Hello world, Xamarin is exciting.. boom.."); } catch (Exception ed) { Console.WriteLine(ed.Message); } } ); }; }
static void Main(string[] args) { #if NET_4_0 try { TwitterServiceProvider twitterServiceProvider = new TwitterServiceProvider(TwitterConsumerKey, TwitterConsumerSecret); /* OAuth 'dance' */ // Authentication using Out-of-band/PIN Code Authentication Console.Write("Getting request token..."); OAuthToken oauthToken = twitterServiceProvider.OAuthOperations.FetchRequestTokenAsync("oob", null).Result; Console.WriteLine("Done"); string authenticateUrl = twitterServiceProvider.OAuthOperations.BuildAuthorizeUrl(oauthToken.Value, null); Console.WriteLine("Redirect user for authentication: " + authenticateUrl); Process.Start(authenticateUrl); Console.WriteLine("Enter PIN Code from Twitter authorization page:"); string pinCode = Console.ReadLine(); Console.Write("Getting access token..."); AuthorizedRequestToken requestToken = new AuthorizedRequestToken(oauthToken, pinCode); OAuthToken oauthAccessToken = twitterServiceProvider.OAuthOperations.ExchangeForAccessTokenAsync(requestToken, null).Result; Console.WriteLine("Done"); /* API */ ITwitter twitter = twitterServiceProvider.GetApi(oauthAccessToken.Value, oauthAccessToken.Secret); Console.WriteLine("Enter your status message:"); string message = Console.ReadLine(); twitter.UpdateStatusAsync(message).Wait(); Console.WriteLine("Status updated!"); } catch (AggregateException ae) { ae.Handle(ex => { if (ex is HttpResponseException) { Console.WriteLine(ex.Message); Console.WriteLine(((HttpResponseException)ex).GetResponseBodyAsString()); return(true); } return(false); }); } #else try { TwitterServiceProvider twitterServiceProvider = new TwitterServiceProvider(TwitterConsumerKey, TwitterConsumerSecret); /* OAuth 'dance' */ // Authentication using Out-of-band/PIN Code Authentication Console.Write("Getting request token..."); OAuthToken oauthToken = twitterServiceProvider.OAuthOperations.FetchRequestToken("oob", null); Console.WriteLine("Done"); string authenticateUrl = twitterServiceProvider.OAuthOperations.BuildAuthorizeUrl(oauthToken.Value, null); Console.WriteLine("Redirect user for authentication: " + authenticateUrl); Process.Start(authenticateUrl); Console.WriteLine("Enter PIN Code from Twitter authorization page:"); string pinCode = Console.ReadLine(); Console.Write("Getting access token..."); AuthorizedRequestToken requestToken = new AuthorizedRequestToken(oauthToken, pinCode); OAuthToken oauthAccessToken = twitterServiceProvider.OAuthOperations.ExchangeForAccessToken(requestToken, null); Console.WriteLine("Done"); /* API */ ITwitter twitter = twitterServiceProvider.GetApi(oauthAccessToken.Value, oauthAccessToken.Secret); Console.WriteLine("Enter your status message:"); string message = Console.ReadLine(); twitter.UpdateStatus(message); Console.WriteLine("Status updated!"); } catch (HttpResponseException ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.GetResponseBodyAsString()); } #endif catch (Exception ex) { Console.WriteLine(ex); } finally { Console.WriteLine("--- hit <return> to quit ---"); Console.ReadLine(); } }
public void UpdateStatus(AccessToken accessToken, string status) { twitter.UpdateStatus(accessToken, status); }