fix: API Key Google Sheets (remplace service account) + signatures routes GET
This commit is contained in:
@@ -2,7 +2,9 @@ import { NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import type { SyncHistoryEntry, SyncLogEntry } from '@/types/sync'
|
||||
|
||||
export async function GET() {
|
||||
import { NextRequest } from 'next/server'
|
||||
|
||||
export async function GET(_req: NextRequest) {
|
||||
try {
|
||||
const runs = await prisma.syncRun.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
|
||||
@@ -3,7 +3,9 @@ import { readSheetProducts } from '@/lib/googleSheets'
|
||||
import { fetchAllShopifyProducts } from '@/lib/shopifySync'
|
||||
import { computeDiff } from '@/lib/syncDiff'
|
||||
|
||||
export async function GET() {
|
||||
import { NextRequest } from 'next/server'
|
||||
|
||||
export async function GET(_req: NextRequest) {
|
||||
try {
|
||||
const [sheetProducts, shopifyProducts] = await Promise.all([
|
||||
readSheetProducts(),
|
||||
|
||||
+14
-19
@@ -37,33 +37,28 @@ export function parseSheetRows(rows: string[][]): SyncProduct[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit tous les produits depuis Google Sheets via l'API v4.
|
||||
* Utilise un service account (GOOGLE_SERVICE_ACCOUNT_EMAIL + GOOGLE_PRIVATE_KEY).
|
||||
* Lit tous les produits depuis Google Sheets via l'API v4 (API Key publique).
|
||||
* Le fichier Sheets doit être partagé en lecture ("Toute personne avec le lien").
|
||||
*/
|
||||
export async function readSheetProducts(): Promise<SyncProduct[]> {
|
||||
const email = process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL
|
||||
const key = process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n')
|
||||
const apiKey = process.env.GOOGLE_API_KEY
|
||||
const sheetId = process.env.GOOGLE_SHEETS_ID
|
||||
const sheetName = process.env.GOOGLE_SHEETS_TAB ?? 'Produits'
|
||||
|
||||
if (!email || !key || !sheetId) {
|
||||
throw new Error('Variables GOOGLE_SERVICE_ACCOUNT_EMAIL, GOOGLE_PRIVATE_KEY et GOOGLE_SHEETS_ID requises')
|
||||
if (!apiKey || !sheetId) {
|
||||
throw new Error('Variables GOOGLE_API_KEY et GOOGLE_SHEETS_ID requises')
|
||||
}
|
||||
|
||||
const { google } = await import('googleapis')
|
||||
const range = encodeURIComponent(`${sheetName}!A2:F`)
|
||||
const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${range}?key=${apiKey}`
|
||||
|
||||
const auth = new google.auth.GoogleAuth({
|
||||
credentials: { client_email: email, private_key: key },
|
||||
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'],
|
||||
})
|
||||
const res = await fetch(url)
|
||||
if (!res.ok) {
|
||||
const err = await res.text()
|
||||
throw new Error(`Google Sheets API error: ${res.status} — ${err}`)
|
||||
}
|
||||
|
||||
const sheets = google.sheets({ version: API_VERSION, auth })
|
||||
|
||||
const response = await sheets.spreadsheets.values.get({
|
||||
spreadsheetId: sheetId,
|
||||
range: `${sheetName}!A2:F`, // A2 = skip header row
|
||||
})
|
||||
|
||||
const rows = (response.data.values ?? []) as string[][]
|
||||
const data = await res.json()
|
||||
const rows = (data.values ?? []) as string[][]
|
||||
return parseSheetRows(rows)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user