2024-09-18 07:28:57 +05:30
|
|
|
import { Router } from 'express';
|
2024-09-17 20:38:32 +05:30
|
|
|
import { google, sheets_v4 } from "googleapis";
|
2024-09-17 20:39:42 +05:30
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2024-09-17 20:38:32 +05:30
|
|
|
|
|
|
|
|
export const router = Router()
|
|
|
|
|
|
|
|
|
|
router.post('/upload-credentials', (req, res) => {
|
|
|
|
|
const credentials = req.body.credentials;
|
2024-09-17 20:58:30 +05:30
|
|
|
|
2024-09-17 20:38:32 +05:30
|
|
|
if (!credentials) {
|
2024-09-17 20:58:30 +05:30
|
|
|
return res.status(400).json({ message: 'Credentials are required.' });
|
2024-09-17 20:39:42 +05:30
|
|
|
}
|
|
|
|
|
// Todo: Store the credentials in a secure place (for test, we store them locally)
|
2024-09-17 20:58:30 +05:30
|
|
|
const storedCredentialsPath = path.join(__dirname, 'service_account_credentials.json');
|
|
|
|
|
fs.writeFileSync(storedCredentialsPath, JSON.stringify(credentials));
|
2024-09-17 20:39:42 +05:30
|
|
|
|
2024-09-17 20:58:30 +05:30
|
|
|
res.status(200).json({ message: 'Service Account credentials saved successfully.' });
|
2024-09-17 20:47:36 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('/write-to-sheet', async (req, res) => {
|
|
|
|
|
try {
|
2024-09-17 21:10:01 +05:30
|
|
|
const { spreadsheetId, range } = req.body;
|
|
|
|
|
|
|
|
|
|
// Todo: remove this. This is just for testing purposes.
|
|
|
|
|
const values = [
|
|
|
|
|
['Scraped Data 1', 'More Data'],
|
|
|
|
|
['Scraped Data 2', 'More Data'],
|
|
|
|
|
];
|
2024-09-17 20:58:30 +05:30
|
|
|
|
2024-09-17 23:50:57 +05:30
|
|
|
const resource = {
|
|
|
|
|
values,
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-17 20:58:30 +05:30
|
|
|
// Load the stored credentials
|
|
|
|
|
const credentialsPath = path.join(__dirname, 'service_account_credentials.json');
|
|
|
|
|
if (!fs.existsSync(credentialsPath)) {
|
|
|
|
|
return res.status(400).json({ message: 'No credentials found. Please provide credentials first.' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const credentials = JSON.parse(fs.readFileSync(credentialsPath, 'utf-8'));
|
|
|
|
|
|
|
|
|
|
// Authenticate with Google using the service account credentials
|
|
|
|
|
const auth = new google.auth.GoogleAuth({
|
2024-09-17 23:50:57 +05:30
|
|
|
credentials: {
|
|
|
|
|
client_email: credentials.client_email,
|
|
|
|
|
private_key: credentials.private_key,
|
|
|
|
|
},
|
2024-09-17 20:58:30 +05:30
|
|
|
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
|
|
|
|
});
|
2024-09-17 23:50:57 +05:30
|
|
|
const authToken = await auth.getClient();
|
|
|
|
|
console.log('authToken:', authToken);
|
|
|
|
|
// return authToken;
|
2024-09-17 20:58:30 +05:30
|
|
|
|
|
|
|
|
const sheets = google.sheets({ version: 'v4', auth });
|
|
|
|
|
|
|
|
|
|
// Write data to the provided Google Sheet and range
|
|
|
|
|
await sheets.spreadsheets.values.append({
|
|
|
|
|
spreadsheetId,
|
|
|
|
|
range,
|
|
|
|
|
valueInputOption: 'USER_ENTERED',
|
2024-09-17 23:50:57 +05:30
|
|
|
requestBody: resource,
|
|
|
|
|
});
|
2024-09-17 20:58:30 +05:30
|
|
|
|
|
|
|
|
res.status(200).json({ message: 'Data written to Google Sheet successfully.' });
|
2024-09-17 20:47:36 +05:30
|
|
|
} catch (error) {
|
2024-09-17 20:58:30 +05:30
|
|
|
console.error('Error writing to sheet:', error);
|
|
|
|
|
res.status(500).json({ message: 'Failed to write to Google Sheet.', error });
|
2024-09-17 20:47:36 +05:30
|
|
|
}
|
2024-09-17 20:58:30 +05:30
|
|
|
});
|