Advertisement
weberc2

simpletweet.vala

May 19th, 2012
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.95 KB | None | 0 0
  1. using Rest;
  2.  
  3. public class SimpleTweet {
  4.     private static const string CONSUMER_KEY = "#######################"; // this comes from your app's twitter account page
  5.     private static const string CONSUMER_SECRET = "########################################"; // this comes from your app's twitter account page
  6.     private static const string URL_FORMAT = "https://api.twitter.com";
  7.     private static const string REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token";
  8.  
  9.     private static const string FUNCTION_ACCESS_TOKEN = "oauth/access_token";
  10.     private static const string FUNCTION_STATUSES_UPDATE = "1/statuses/update.xml";
  11.  
  12.     private static const string PARAM_STATUS = "status";
  13.  
  14.     public static int main(string[] args) {
  15.         // make sure there is a message to tweet
  16.         if(args[1] == null || args[1] == "") {
  17.             stdout.printf("No tweet message specified.\n");
  18.             return 0;
  19.         }
  20.  
  21.         /** Authenticating with Twitter */
  22.         // initialize proxy
  23.         var proxy = new OAuthProxy(CONSUMER_KEY, CONSUMER_SECRET, URL_FORMAT, false);
  24.  
  25.         // request token
  26.         try {
  27.             proxy.request_token("oauth/request_token", "oob");
  28.         } catch (Error e) {
  29.             stderr.printf("Couldn't get request token: %s\n", e.message);
  30.             return 1;
  31.         }
  32.  
  33.         // prompt user for pin
  34.         stdout.printf("Go to http://twitter.com/oauth/authorize?oauth_token=%s then enter the PIN\n", proxy.get_token());
  35.         string pin = stdin.read_line();
  36.  
  37.         // access token
  38.         try { proxy.access_token(FUNCTION_ACCESS_TOKEN, pin); }
  39.         catch (Error e) {
  40.             stderr.printf("Couldn't get access token: %s\n", e.message);
  41.             return 1;
  42.         }
  43.  
  44.         /** sending the tweet */
  45.         // setup call
  46.         ProxyCall call = proxy.new_call();
  47.         call.set_function(FUNCTION_STATUSES_UPDATE);
  48.         call.set_method("POST");
  49.         call.add_param(PARAM_STATUS, args[1]);
  50.         try { call.sync(); } catch (Error e) {
  51.             stderr.printf("Cannot make call: %s\n", e.message);
  52.             return 1;
  53.         }
  54.  
  55.         // print success
  56.         stdout.printf("Tweet succeeded!\n");
  57.  
  58.         return 0;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement