sfsdf
This commit is contained in:
parent
c34f0391b3
commit
272f325d98
|
|
@ -1,4 +1,4 @@
|
|||
// qbo_helper.js - FINALE VERSION
|
||||
// qbo_helper.js - FIX FÜR ERROR 3200
|
||||
require('dotenv').config();
|
||||
const OAuthClient = require('intuit-oauth');
|
||||
const fs = require('fs');
|
||||
|
|
@ -19,7 +19,6 @@ const getOAuthClient = () => {
|
|||
let savedToken = null;
|
||||
try {
|
||||
if (fs.existsSync(tokenFile)) {
|
||||
// Wir lesen nur, wenn es kein Ordner ist (für lokale Tests ohne Docker)
|
||||
const stat = fs.statSync(tokenFile);
|
||||
if (stat.isFile()) {
|
||||
const content = fs.readFileSync(tokenFile, 'utf8');
|
||||
|
|
@ -74,34 +73,37 @@ async function makeQboApiCall(requestOptions) {
|
|||
}
|
||||
};
|
||||
|
||||
// --- WICHTIG: KEINE isAccessTokenValid() PRÜFUNG HIER! ---
|
||||
// Wir vertrauen darauf, dass der Token (egal ob Datei oder .env) funktioniert.
|
||||
// Wir refreshen nur, wenn QBO uns abweist.
|
||||
|
||||
try {
|
||||
const response = await client.makeApiCall(requestOptions);
|
||||
|
||||
// Prüfen, ob die Antwort JSON ist (manche Auth-Fehler sind HTML/Text)
|
||||
const data = response.getJson ? response.getJson() : response.json;
|
||||
|
||||
if (data.fault && data.fault.error) {
|
||||
const errorCode = data.fault.error[0].code;
|
||||
if (errorCode === '3202' || errorCode === '3100') {
|
||||
|
||||
// --- FIX: 3200 (Auth Failed) HINZUGEFÜGT ---
|
||||
if (errorCode === '3200' || errorCode === '3202' || errorCode === '3100') {
|
||||
console.log(`⚠️ QBO meldet Token-Fehler (${errorCode}). Versuche Refresh und Retry...`);
|
||||
await doRefresh();
|
||||
// Retry mit neuem Token
|
||||
return await client.makeApiCall(requestOptions);
|
||||
}
|
||||
throw new Error(`QBO API Error ${errorCode}: ${data.fault.error[0].message}`);
|
||||
}
|
||||
|
||||
// Bei Erfolg: Speichern (falls sich intern was geändert hat durch die Lib)
|
||||
saveTokens();
|
||||
return response;
|
||||
|
||||
} catch (e) {
|
||||
const isAuthError = e.response?.status === 401 || (e.authResponse && e.authResponse.response && e.authResponse.response.status === 401);
|
||||
// HTTP 401 Unauthorized fangen (falls die Lib wirft, statt data.fault zurückzugeben)
|
||||
const isAuthError =
|
||||
e.response?.status === 401 ||
|
||||
(e.authResponse && e.authResponse.response && e.authResponse.response.status === 401) ||
|
||||
e.message?.includes('AuthenticationFailed');
|
||||
|
||||
if (isAuthError) {
|
||||
console.log("⚠️ 401 Unauthorized erhalten. Versuche Refresh und Retry...");
|
||||
console.log("⚠️ 401 Unauthorized / AuthFailed erhalten. Versuche Refresh und Retry...");
|
||||
await doRefresh();
|
||||
return await client.makeApiCall(requestOptions);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue