let
// ====== Paramètres ======
AuthBaseUrl = paramAuthBaseURL,
LoginPath = paramLoginPath,
ApiBaseUrl = paramApiBaseUrl,
ApiPath = paramApiPath,
// Pour obtenir le token, utiliser Email & Mot de passe...
Email = "un email",
Password = "un mot de passe",
// ...ou la clé API
APIKey = "une clé API",
// ====== 1) Auth : POST JSON => réponse JSON avec token ======
LoginBody =
Json.FromValue([
email = Email,
password = Password
]),
LoginResponse =
Json.Document(
Web.Contents(
AuthBaseUrl,
[
RelativePath = LoginPath,
Headers = [
#"Content-Type" = "application/json",
#"X-API-Key"= APIKey,
Accept = "application/json"
],
Content = LoginBody,
ManualStatusHandling = {400,401,403,500}
]
)
),
// Adapter le champ selon la réponse réelle : token / access_token / data[token]...
Token =
if Record.HasFields(LoginResponse, "token") then LoginResponse[token]
else if Record.HasFields(LoginResponse, "access_token") then LoginResponse[access_token]
else error "Champ token introuvable dans la réponse d'authentification.",
// ====== 2) Appel API avec Bearer ======
//JsonDoc = Json.Document(WebResult),
WebContent =
Web.Contents(
ApiBaseUrl,
[
RelativePath = ApiPath,
Headers = [
Authorization = "Bearer " & Token,
Accept = "application/json"
],
Timeout = #duration(0,0,2,0),
ManualStatusHandling = {400,401,403,500}
]
),
ApiResponse = Json.Document(WebContent),
// ====== 2) Appel API avec Bearer ======
data1 = ApiResponse[data],
#"Converti en table" = Table.FromList(data1, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
#"Converti en table"