#twitter #oauth #api-bindings

twapi-reqwest

reqwest 所使用的 Twitter OAuth 库

9 个版本

0.3.0 2023年2月28日
0.2.3 2022年8月16日
0.2.2 2021年9月14日
0.2.0 2021年3月26日
0.1.2 2020年2月2日

#10#twitter

Download history 325/week @ 2024-03-29 354/week @ 2024-04-05 205/week @ 2024-04-12 217/week @ 2024-04-19 196/week @ 2024-04-26 148/week @ 2024-05-03 198/week @ 2024-05-10 136/week @ 2024-05-17 269/week @ 2024-05-24 81/week @ 2024-05-31 91/week @ 2024-06-07 251/week @ 2024-06-14 196/week @ 2024-06-21 100/week @ 2024-06-28 224/week @ 2024-07-05 89/week @ 2024-07-12

630 每月下载量
用于 twapi

MIT/Apache

31KB
737

twapi-reqwest-rs

reqwest 所使用的 Twitter OAuth 库。

文档

特性

  • 异步/Await
  • 应用仅认证
  • 用户认证
  • Oauth1.0 认证
  • Oauth2.0 认证
  • 支持 JSON(例如 dm_event、welcome_message、media_metadata 等)
  • 支持多部分(例如 post_media_upload)

变更

v0.3.0 (2023/02/28)

  • 添加超时设置

v0.2.3 (2022/08/16)

  • 支持 reqwest rustls-tls 功能

v0.2.2 (2021/09/14)

  • oauth 解析 OAuth body 检查响应 is_success

v0.2.1 (2021/09/14)

  • oauth 解析 OAuth body 将原始 body 添加到 HashMap

v0.2.0 (2021/03/26)

  • 更新 reqwest 0.11
  • 添加 Client 结构体

示例

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);
}

依赖

~8–24MB
~344K SLoC