4 releases

0.2.1 Feb 25, 2024
0.2.0 Feb 16, 2024
0.1.1 Feb 11, 2024
0.1.0 Feb 10, 2024

#555 in Web programming

Download history 86/week @ 2024-02-10 40/week @ 2024-02-17 130/week @ 2024-02-24 12/week @ 2024-03-02 6/week @ 2024-03-09 162/week @ 2024-03-23 1/week @ 2024-03-30

165 downloads per month

MIT and GPL-3.0-only

45KB
706 lines

strava-client-rs

crates.io docs.rs

Strava API Client in Rust that uses OAuth2 to get and refresh the access token

Example

use strava_client_rs::{api::{auth, athlete}};
use strava_client_rs::util::auth_config;
use std::env;
use std::path::Path;

fn main() {
    //Get the access token from the config file or get a new one                                     
    let config_file = env::var("STRAVA_CONFIG_FILE").unwrap_or_else(|_| "config.json".to_string());
    let access_token = get_access_token(config_file).unwrap();
    let athlete = athlete::get_athlete(access_token.as_str()).unwrap();
    println!("Athlete: {:?}\n", athlete);
    let athlete_id = athlete.id.to_string();
}

// Get the access token from the config file or get a new one                                       
fn get_access_token(config_file: String) -> Result<String, String> {
    let client_id =
        env::var("STRAVA_CLIENT_ID").expect("Missing the STRAVA_CLIENT_ID environment variable.");
    let client_secret = env::var("STRAVA_CLIENT_SECRET")
        .expect("Missing the STRAVA_CLIENT_SECRET environment variable.");
    let auth_url = "http://www.strava.com/oauth/authorize";
    let token_url = "https://www.strava.com/oauth/token";

    // Setup default config for auth                                                                
    let mut config = auth::Config::new(
        client_id.to_string(),
        client_secret.to_string(),
        Default::default(), // no refresh token so set to default which is none                     
        auth_url.to_string(),
        token_url.to_string(),
    );

    // Check if the config file exists and get the access token or get a new one                    
    if Path::new(&config_file).exists() {
        config.refresh_token = Some(auth_config::config_file::load_config().refresh_token);
        let refresh_access_token = auth::get_refresh_token(config);
        Ok(refresh_access_token.unwrap().to_string())
    } else {
        let access_token = auth::get_authorization(config);
        Ok(access_token.unwrap().to_string()) }
}

Versions

Disclaimer

This library is not affiliated with Strava. Use at your own risk. This is very much a work in progress. In the current release it is read only for athlete, gear, club, and activities. It will also update an athlete weight in kg.

Dependencies

~6–21MB
~309K SLoC