From 32ac30631dfeba7ec14c38c76eaa7b88db63ec89 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 20 Apr 2025 13:12:13 +0200 Subject: [PATCH] carnet api --- app/api/nextcloud/status/route.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/api/nextcloud/status/route.ts b/app/api/nextcloud/status/route.ts index 694148fb..b1f765b4 100644 --- a/app/api/nextcloud/status/route.ts +++ b/app/api/nextcloud/status/route.ts @@ -5,7 +5,11 @@ import { DOMParser } from '@xmldom/xmldom'; async function getNextcloudAccessToken(clientId: string, clientSecret: string) { const nextcloudUrl = process.env.NEXTCLOUD_URL; - const tokenEndpoint = `${nextcloudUrl}/apps/oauth2/api/v1/token`; + if (!nextcloudUrl) { + throw new Error('NEXTCLOUD_URL is not defined'); + } + + const tokenEndpoint = `${nextcloudUrl}/index.php/apps/oauth2/api/v1/token`; const params = new URLSearchParams({ grant_type: 'client_credentials', @@ -17,15 +21,22 @@ async function getNextcloudAccessToken(clientId: string, clientSecret: string) { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', + 'Accept': 'application/json', }, body: params.toString(), }); if (!response.ok) { - throw new Error('Failed to get Nextcloud access token'); + const errorText = await response.text(); + console.error('Nextcloud token response:', errorText); + throw new Error(`Failed to get Nextcloud access token: ${response.status} ${errorText}`); } const data = await response.json(); + if (!data.access_token) { + throw new Error('No access token in response'); + } + return data.access_token; }