1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use reqwest::{Client, Error};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Debug, Serialize)]
pub struct OAuthToken {
    pub access_token: String,
    pub token_type: String,
    pub expires_in: usize,
}

/// To retrieve a token, you need to provide your client_id and client_secret
///
/// ```rust
/// let token = battle_net_oauth::get_oauth_token("client_id", "client_secret");
/// ```
pub fn get_oauth_token(client_id: &str, client_secret: &str) -> Result<OAuthToken, Error> {
    let token_client = Client::new();

    let resp: OAuthToken = token_client
        .post("https://eu.battle.net/oauth/token")
        .basic_auth(client_id, Some(client_secret))
        .form(&[("grant_type", "client_credentials")])
        .send()?
        .error_for_status()?
        .json()?;

    Ok(resp)
}