#大学 #API #API绑定 #usos #波兰

usos-rs

提供方便地认证到USOS API的方式。

1个稳定版本

1.0.0 2024年3月13日

#598身份验证

AGPL-3.0-or-later

17KB
227

usos-rs

usos-rs crate提供方便地认证到USOS API的方式。

示例

获取用户的首名

use reqwest::Url;
use std::error::Error;
use std::{collections::HashSet, io};
use usos_rs::{self, Scope, UsosRs};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let consumer_key = "consumer key";
    let consumer_secret = "consumer secret";
    let api_url = Url::parse("api url")?;
    let usos = UsosRs::new(api_url, consumer_key.to_owned(), consumer_secret.to_owned())?;

    let scopes = HashSet::from([Scope::PersonalData]);
    let u_user = usos.get_unauthorized_user(Some(&scopes)).await?;

    println!("{}", u_user.authorize_url());
    let mut user_input = String::new();
    io::stdin().read_line(&mut user_input)?;
    let pin = user_input.trim();
    let user = u_user.authorize(pin).await.map_err(|(_, e)| e)?;

    let response = user
        .reqwest_client()
        .post(user.api_url().join("/services/users/user")?)
        .query(&[("fields", "first_name")])
        .send()
        .await?;

    println!("{}", response.text().await?);

    Ok(())
}

获取已授权用户的首名

use reqwest::Url;
use std::error::Error;
use usos_rs::{self, AccessToken, UsosRs};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let consumer_key = "consumer key";
    let consumer_secret = "consumer secret";
    let api_url = Url::parse("api url")?;
    let usos = UsosRs::new(api_url, consumer_key.to_owned(), consumer_secret.to_owned())?;

    let user = usos
        .get_authorized_user(AccessToken {
            token: "access token".to_owned(),
            token_secret: "access token secret".to_owned(),
        })
        .await;

    let response = user
        .reqwest_client()
        .post(user.api_url().join("/services/users/user")?)
        .query(&[("fields", "first_name")])
        .send()
        .await?;

    println!("{}", response.text().await?);

    Ok(())
}

许可证

AGPLv3+ © Filip K.

依赖

~6–18MB
~275K SLoC