Last active
May 10, 2016 15:56
-
-
Save deadcheat/155d2181567fc523472808979f3bd426 to your computer and use it in GitHub Desktop.
Example receive a callback from twitter-oauth, and tweet strings given by args.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate twitter_api as twitter; | |
extern crate oauth_client as oauth; | |
extern crate aurelius; | |
extern crate iron; | |
extern crate urlencoded; | |
use aurelius::browser; | |
use iron::prelude::*; | |
use oauth::Token; | |
use std::env; | |
use std::process; | |
use urlencoded::UrlEncodedQuery; | |
fn main() { | |
let consumer_key = "your consumer key"; | |
let consumer_secret = "secret"; | |
let consumer = Token::new(consumer_key, consumer_secret); | |
let request = twitter::get_request_token(&consumer).unwrap(); | |
let auth_url = twitter::get_authorize_url(&request); | |
println!("open the following url:{}", auth_url); | |
let _ = browser::open(&auth_url); | |
Iron::new(move |req: &mut Request| { | |
match req.get_ref::<UrlEncodedQuery>() { | |
Ok(ref hashmap) => { | |
println!("Parsed GET request query string:\n {:?}", hashmap); | |
let vec_oauth_verifier = hashmap.get("oauth_verifier").unwrap(); | |
let oauth_verifier = &vec_oauth_verifier[0]; | |
println!("oauth_verifier: {:?}", oauth_verifier); | |
let access = twitter::get_access_token(&consumer, &request, &oauth_verifier).unwrap(); | |
// タイムラインの取得 | |
let tweets = twitter::get_last_tweets(&consumer, &access).unwrap(); | |
for tweet in tweets { | |
println!("{} - {}", tweet.created_at, tweet.text) | |
} | |
// 引数で与えられた文字列を投稿する | |
let args = env::args(); | |
let mut argv: Vec<String> = args.collect(); | |
argv.reverse(); | |
let _ = argv.pop(); | |
argv.reverse(); | |
for arg in argv { | |
twitter::update_status(&consumer, &access, &arg).unwrap(); | |
} | |
}, | |
Err(ref e) => { | |
println!("{:?}", e); | |
process::exit(1); | |
} | |
}; | |
process::exit(0) | |
}).http("callback-host:port").unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment