3个不稳定版本

0.3.0 2020年4月27日
0.1.1 2019年8月13日
0.1.0 2019年8月13日

#9 in #playstation

MIT许可证

59KB
1.5K SLoC

简单的PSN API包装器


特性

使用reqwest作为HTTP客户端连接到PSN网络。
获取PSN用户资料、奖杯、游戏信息
接收/发送PSN消息。
获取PSN商店信息。



lib.rs:

简单的PSN API包装器。

它使用异步HTTP客户端(hyper::Client)与官方PSN API通信。

一些基础知识:该包使用npsso代码登录到PSN网络并获取一对access_tokenrefresh_token作为响应。 如何获取uuid和两步验证令牌 access_token在过期前大约持续一小时,大多数其他PSN API调用都需要它(PSN商店API不需要任何令牌即可访问)。 refresh_token持续时间更长,用于在它过期后/之前生成新的access_token。

  • 注意:官方PSN API有速率限制,所以最好不要在短时间内进行大量调用。代理示例(使用此库的最佳实践)展示了如何实现高并发并有效地对抗速率限制。

基本示例

use psn_api_rs::{psn::PSN, types::PSNInner, traits::PSNRequest, models::PSNUser};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let refresh_token = String::from("your refresh token");
    let npsso = String::from("your npsso");

    let client = PSN::new_client().expect("Failed to build http client");

    // construct a PSNInner object,add credentials and call auth to generate tokens.
    let mut psn_inner = PSNInner::new();
    psn_inner.set_region("us".to_owned()) // <- set to a psn region server suit your case. you can leave it as default which is hk
            .set_lang("en".to_owned()) // <- set to a language you want the response to be. default is en
            .set_self_online_id(String::from("Your Login account PSN online_id")) // <- this is used to generate new message thread. safe to leave unset if you don't need to send any PSN message.
            .add_refresh_token(refresh_token) // <- If refresh_token is provided then it's safe to ignore add_npsso and call .auth() directly.
            .add_npsso(npsso); // <- npsso is used only when refresh_token is not working or not provided.

    psn_inner = psn_inner
            .auth()
            .await
            .unwrap_or_else(|e| panic!("{:?}", e));

    println!(
        "Authentication Success! These are your info from PSN network: \r\n{:#?} ",
        psn_inner
    );

    let user = psn_inner
            .get_profile::<PSNUser>(&client, "Hakoom")
            .await
            .unwrap_or_else(|e| panic!("{:?}", e));

    println!(
        "Example finished. Got user info : \r\n{:#?}",
        user
    );

    Ok(())
    // psn struct is dropped at this point so it's better to store your access_token and refresh_token here to make them reusable.
}

依赖项

~9–13MB
~265K SLoC