summaryrefslogtreecommitdiff
path: root/src/strava.rs
blob: dcd8dc1b798122a4924dd7877a1fa5606c620eb2 (plain)
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
29
30
31
32
33
34
35
use reqwest;
use serde::Deserialize;
use serde::Serialize;

#[derive(Serialize, Deserialize, Debug)]
pub struct AthleteSummary {
    id: i64,
    username: String,
    firstname: String,
    lastname: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Token {
    expires_in: i64,
    refresh_token: String,
    access_token: String,
    athlete: AthleteSummary,
}

pub fn exchange_token(
    client_id: &str,
    client_secret: &str,
    code: &str,
) -> Result<Token, reqwest::Error> {
    let client = reqwest::blocking::Client::new();
    let params = [
        ("client_id", client_id),
        ("client_secret", client_secret),
        ("code", code),
    ];
    let uri = "https://www.strava.com/oauth/token";
    let req = client.post(uri).form(&params);
    req.send().map(|r| r.json())?
}