Crates.io | twapi-reqwest |
lib.rs | twapi-reqwest |
version | 0.3.0 |
source | src |
created_at | 2020-02-01 06:01:22.541094 |
updated_at | 2023-02-28 07:52:13.499593 |
description | Twitter OAuth library used by reqwest. |
homepage | |
repository | https://github.com/aoyagikouhei/twapi-reqwest-rs |
max_upload_size | |
id | 203851 |
size | 30,569 |
Twitter OAuth library used by reqwest.
use twapi_reqwest::*;
use std::env;
use std::io::{BufReader, Cursor, Read};
#[tokio::main]
async fn main() {
// OAuth2.0 Authentication
let consumer_key = env::var("CONSUMER_KEY").unwrap();
let consumer_secret = env::var("CONSUMER_SECRET").unwrap();
let bearer_token = oauth::get_bearer_token(&consumer_key, &consumer_secret).await.unwrap().unwrap();
// search(Application Only Authentication)
let res: serde_json::Value = v2::get(
"https://api.twitter.com/1.1/search/tweets.json",
&vec![("q", "東京&埼玉"), ("count", "2")],
&bearer_token,
None,
)
.await
.unwrap()
.json()
.await
.unwrap();
println!("{:?}", res);
// OAuth1.0 Authentication
let consumer_key = env::var("CONSUMER_KEY").unwrap();
let consumer_secret = env::var("CONSUMER_SECRET").unwrap();
let access_key = env::var("ACCESS_KEY").unwrap();
let access_secret = env::var("ACCESS_SECRET").unwrap();
// home_timeline
let url = "https://api.twitter.com/1.1/statuses/home_timeline.json";
let query_options = vec![("count", "2")];
let res: serde_json::Value = v1::get(
url,
&query_options,
&consumer_key,
&consumer_secret,
&access_key,
&access_secret,
None,
)
.await
.unwrap()
.json()
.await
.unwrap();
println!("{:?}", res);
// statuses/update
let url = "https://api.twitter.com/1.1/statuses/update.json";
let form_options = vec![
("status", "!\"'#$%&\\()+,/:;<=>?@[\\]^`{|}~;-._* 全部"),
("in_reply_to_status_id", "1178811297455935488"),
];
let res: serde_json::Value = v1::post(
url,
&vec![],
&form_options,
&consumer_key,
&consumer_secret,
&access_key,
&access_secret,
None,
)
.await
.unwrap()
.json()
.await
.unwrap();
println!("{:?}", res);
// direct_messages new(JSON support)
let url = "https://api.twitter.com/1.1/direct_messages/events/new.json";
let data = r#"{
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": "19522946"
},
"message_data": {
"text": "予定表〜①ハンカクだ!"
}
}
}
}"#;
let data: serde_json::Value = serde_json::from_str(data).unwrap();
let res: serde_json::Value = v1::json(
url,
&vec![],
&data,
&consumer_key,
&consumer_secret,
&access_key,
&access_secret,
None,
)
.await
.unwrap()
.json()
.await
.unwrap();
println!("{:?}", res);
// media/upload(Multipart support)
let metadata = std::fs::metadata("test.jpg").unwrap();
let file_size = metadata.len();
let f = std::fs::File::open("test.jpg").unwrap();
let mut cursor = Cursor::new(vec![0; file_size as usize]);
let mut reader = BufReader::new(f);
reader.read(cursor.get_mut()).unwrap();
let part = reqwest::multipart::Part::bytes(cursor.into_inner());
let data = reqwest::multipart::Form::new().part("media", part);
let url = "https://upload.twitter.com/1.1/media/upload.json";
let res: serde_json::Value = v1::multipart(
url,
&vec![],
data,
&consumer_key,
&consumer_secret,
&access_key,
&access_secret,
None,
)
.await
.unwrap()
.json()
.await
.unwrap();
println!("{:?}", res);
}