Échanger le code d'autorisation contre des tokens
strava_exchange_tokenExchange OAuth2 authorization codes from Strava's redirect URL for access and refresh tokens, saving them locally to enable authenticated API requests.
Instructions
Échange le code OAuth2 (depuis l'URL de redirection après autorisation dans le navigateur) contre des tokens d'accès et de rafraîchissement. Les tokens sont sauvegardés localement.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Le code d'autorisation depuis l'URL de redirection Strava |
Implementation Reference
- src/auth/authTools.ts:46-75 (registration)Registration of the 'strava_exchange_token' tool via server.registerTool. Defines the input schema (code string) and the handler which calls exchangeCodeForTokens.
server.registerTool( "strava_exchange_token", { title: "Échanger le code d'autorisation contre des tokens", description: "Échange le code OAuth2 (depuis l'URL de redirection après autorisation dans le navigateur) " + "contre des tokens d'accès et de rafraîchissement. Les tokens sont sauvegardés localement.", inputSchema: z.object({ code: z.string().describe("Le code d'autorisation depuis l'URL de redirection Strava"), }), }, async ({ code }) => { try { const tokens = await exchangeCodeForTokens(code); return { content: [ { type: "text", text: `✓ Authentifié avec succès ! Athlete ID : ${tokens.athlete_id}.\nTokens sauvegardés dans ${config.tokensFilePath}.`, }, ], }; } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text", text: `Erreur lors de l'échange du token : ${msg}` }], }; } } ); - src/auth/authTools.ts:57-74 (handler)The handler function for strava_exchange_token. Receives { code }, calls exchangeCodeForTokens(code), and returns a success or error message.
async ({ code }) => { try { const tokens = await exchangeCodeForTokens(code); return { content: [ { type: "text", text: `✓ Authentifié avec succès ! Athlete ID : ${tokens.athlete_id}.\nTokens sauvegardés dans ${config.tokensFilePath}.`, }, ], }; } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text", text: `Erreur lors de l'échange du token : ${msg}` }], }; } } - src/auth/authTools.ts:48-55 (schema)Input schema for strava_exchange_token: expects a single 'code' string parameter (the OAuth authorization code from the redirect URL).
{ title: "Échanger le code d'autorisation contre des tokens", description: "Échange le code OAuth2 (depuis l'URL de redirection après autorisation dans le navigateur) " + "contre des tokens d'accès et de rafraîchissement. Les tokens sont sauvegardés localement.", inputSchema: z.object({ code: z.string().describe("Le code d'autorisation depuis l'URL de redirection Strava"), }), - src/auth/oauth.ts:17-32 (helper)The core OAuth helper function 'exchangeCodeForTokens' that posts the authorization code to Strava's token endpoint, saves tokens, and returns them.
export async function exchangeCodeForTokens(code: string): Promise<StravaTokens> { const { data } = await axios.post(STRAVA_TOKEN_URL, { client_id: config.clientId, client_secret: config.clientSecret, code, grant_type: "authorization_code", }); const tokens: StravaTokens = { access_token: data.access_token, refresh_token: data.refresh_token, expires_at: data.expires_at, athlete_id: data.athlete?.id ?? 0, }; saveTokens(config.tokensFilePath, tokens); return tokens; } - src/auth/oauth.ts:1-4 (helper)Dependencies imported by oauth.ts: axios for HTTP, config for Strava credentials, tokenStore for saving tokens, and the StravaTokens type.
import axios from "axios"; import { STRAVA_AUTH_URL, STRAVA_TOKEN_URL, STRAVA_SCOPES, config } from "../config.js"; import { loadTokens, saveTokens } from "./tokenStore.js"; import type { StravaTokens } from "../types.js";