def authenticate(): ## Strava API uses OAuth2, which requires users to manually allow permission, which generates ## a token only valid for a number of hours. ## get API client id and secret from config file config = configparser.ConfigParser() config.read("credentials.cfg") client_id = config.get('credentials', 'client_id') client_secret = config.get('credentials', 'client_secret') client = Client(rate_limiter=limiter.DefaultRateLimiter()) authorize_url = client.authorization_url( client_id=client_id, redirect_uri='http://localhost:8282/authorized') ## getting token -- pretty manual process for now webbrowser.open_new_tab(authorize_url) code = input('Enter Temporary Code: ') code = str(code) ## authenticate using API credntials + token token_response = client.exchange_code_for_token( client_id=client_id, client_secret=client_secret, code=code) return client
def __init__(self, access_token=None, rate_limit_requests=True, rate_limiter=None): """ Initialize a new client object. :param access_token: The token that provides access to a specific Strava account. If empty, assume that this account is not yet authenticated. :type access_token: str :param rate_limit_requests: Whether to apply a rate limiter to the requests. (default True) :type rate_limit_requests: bool :param rate_limiter: A :class:`stravalib.util.limiter.RateLimiter' object to use. If not specified (and rate_limit_requests is True), then :class:`stravalib.util.limiter.DefaultRateLimiter' will be used. :type rate_limiter: callable """ self.log = logging.getLogger('{0.__module__}.{0.__name__}'.format(self.__class__)) if rate_limit_requests: if not rate_limiter: rate_limiter = limiter.DefaultRateLimiter() elif rate_limiter: raise ValueError("Cannot specify rate_limiter object when rate_limit_requests is False") self.protocol = ApiV3(access_token=access_token, rate_limiter=rate_limiter)
def __init__(self, hass, accesstoken, devices): self.hass = hass from stravalib import Client from stravalib.util import limiter self._strava_client = Client(access_token=accesstoken, rate_limiter=limiter.DefaultRateLimiter()) self._athlete = self._strava_client.get_athlete() self._accesstoken = accesstoken self._devices = devices self._athlete_stats = None
def fetch_data(self, *_): """Get the athlete stats.""" stats_fetched_ok = False from requests.exceptions import ConnectionError try: athlete_stats = self._strava_client.get_athlete_stats(athlete_id=self._athlete.id) stats_fetched_ok = True except: _LOGGER.warn("Failed to get athlete stats, re-initiating client") from stravalib import Client from stravalib.util import limiter self._strava_client = Client(access_token=self._accesstoken, rate_limiter=limiter.DefaultRateLimiter()) if stats_fetched_ok: self._athlete_stats = athlete_stats.to_dict() yield from self.update_devices() async_call_later(self.hass, 2*60, self.fetch_data)
import webbrowser import os from datetime import datetime import pandas as pd from stravalib.client import Client from stravalib.util import limiter from process_strava import get_token, refresh_token, save_token from process_strava import strava_clean # This file contains the id and secrets # Trying with a new free account client_id, client_secret = open('secrets-chaya.txt').read().strip().split(',') client = Client(rate_limiter=limiter.DefaultRateLimiter()) # ****This only needs to happen once. Once we have the token we can simply refresh **** path_to_save = os.path.join("access_token.pickle") if not os.path.exists(path_to_save): # Generate the URL # QUESTION: do we need to hide the CLIENT_ID? print( "Authenticating with Strava. Be sure you are logged into Strava before running this!" ) print( "I am launching a web browser. Please return the code following code= in the resulting url." ) print("i know this is clunky but it's a starting place")