Crates.io | strava-client-rs |
lib.rs | strava-client-rs |
version | 0.2.2 |
source | src |
created_at | 2024-02-10 18:52:54.244127 |
updated_at | 2024-09-23 12:55:12.124873 |
description | Strava API Client lib |
homepage | https://github.com/qgriffith/strava-client-rs |
repository | https://github.com/qgriffith/strava-client-rs |
max_upload_size | |
id | 1135124 |
size | 45,863 |
Strava API Client in Rust that uses OAuth2 to get and refresh the access token
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()) }
}
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.