feat: write data to sheet with oauth
This commit is contained in:
@@ -68,32 +68,52 @@ export async function updateGoogleSheet(robotId: string, runId: string) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function writeDataToSheet(fileName: string, spreadsheetId: string, range: string, data: any[]) {
|
export async function writeDataToSheet(robotId: string, spreadsheetId: string, data: any[]) {
|
||||||
try {
|
try {
|
||||||
const integrationCredentialsPath = getIntegrationsFilePath(fileName);
|
const robot = await Robot.findOne({ where: { 'recording_meta.id': robotId } });
|
||||||
const integrationCredentials = JSON.parse(fs.readFileSync(integrationCredentialsPath, 'utf-8'));;
|
|
||||||
|
|
||||||
const auth = new google.auth.GoogleAuth({
|
if (!robot) {
|
||||||
credentials: {
|
throw new Error(`Robot not found for robotId: ${robotId}`);
|
||||||
client_email: integrationCredentials.credentials.client_email,
|
}
|
||||||
private_key: integrationCredentials.credentials.private_key,
|
|
||||||
},
|
if (!robot.google_access_token || !robot.google_refresh_token) {
|
||||||
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
throw new Error('Google Sheets access not configured for user');
|
||||||
|
}
|
||||||
|
|
||||||
|
const oauth2Client = new google.auth.OAuth2(
|
||||||
|
process.env.GOOGLE_CLIENT_ID,
|
||||||
|
process.env.GOOGLE_CLIENT_SECRET,
|
||||||
|
'http://localhost:3000'
|
||||||
|
);
|
||||||
|
|
||||||
|
oauth2Client.setCredentials({
|
||||||
|
access_token: robot.google_access_token,
|
||||||
|
refresh_token: robot.google_refresh_token,
|
||||||
});
|
});
|
||||||
|
|
||||||
const authToken = await auth.getClient();
|
// Refresh the access token if needed
|
||||||
const sheets = google.sheets({ version: 'v4', auth: authToken as any });
|
oauth2Client.on('tokens', async (tokens) => {
|
||||||
|
if (tokens.refresh_token) {
|
||||||
|
await robot.update({ google_refresh_token: tokens.refresh_token });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tokens.access_token) {
|
||||||
|
await robot.update({ google_access_token: tokens.access_token });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const sheets = google.sheets({ version: 'v4', auth: oauth2Client });
|
||||||
|
|
||||||
const resource = { values: data };
|
const resource = { values: data };
|
||||||
|
|
||||||
await sheets.spreadsheets.values.append({
|
await sheets.spreadsheets.values.append({
|
||||||
spreadsheetId,
|
spreadsheetId,
|
||||||
range,
|
range: 'Sheet1!A1',
|
||||||
valueInputOption: 'USER_ENTERED',
|
valueInputOption: 'USER_ENTERED',
|
||||||
requestBody: resource,
|
requestBody: resource,
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.log(`info`, `Data written to Google Sheet: ${spreadsheetId}, Range: ${range}`);
|
logger.log(`info`, `Data written to Google Sheet: ${spreadsheetId}`);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.log(`error`, `Error writing data to Google Sheet: ${error.message}`);
|
logger.log(`error`, `Error writing data to Google Sheet: ${error.message}`);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"fileName": "Load More Test",
|
||||||
|
"spreadsheetId": "1lv1DV_Fs2pqRWje5Go9Lfi4qFRjaVH5a8-wOdduOLpM",
|
||||||
|
"range": "Sheet1!A1:B2",
|
||||||
|
"credentials": {
|
||||||
|
"type": "service_account",
|
||||||
|
"project_id": "maxun-test",
|
||||||
|
"private_key_id": "19ebcba7a1df27c872e9f4944f38c1636f31ab71",
|
||||||
|
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC6JMuPu+4XvObD\npKcRjNqVb/oCgPLQ4EVVe7yr4u7w8RS5bsQ98ef9lSf4YFlBB8mv+iNqcLCpnW+0\nx/rY+GeuHuUsAzpmvqlBA3s6YnOW52bkNLQsPJfHZTSHOn0pO/5WwWwZkGNNpjY/\njhsqWDXG+CkXq5qAt/e6B2DlM1y3nhYVooguNujqhxKYT8GnS75utEDFpHxvcTN+\n/0dYTz/iDQ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oz6C7zaX2+t4JNm+JJ1cCfU5K4XcnMkwV4kYwx\nUWiGcBj6de8b0zqmo8GeeAOtu20elRzygQr6tj6B1JnMN60i+K9QohattxWrGHIJ\natXRyisF7muDDMYqQtI/oOyqIUGLFlLQcovNugaiDwKBgQCRHTfXCu4GyaXmW7du\n1liWLx2W7HWF8e7Lbfo4mw7dR/qaU+LtY+IGFviVmOWmfg5YkstSbnZnJ/h6TMMw\nLQUL4p/gtC+TEKtISU0BUei2T4URGfFNEXybZqD/PKc6FJs0hwolAuuK2AcnvZur\nAclfCbofvdN0owKnHhBFz4o7AA==\n-----END PRIVATE KEY-----\n",
|
||||||
|
"client_email": "maxun-test@maxun-test.iam.gserviceaccount.com",
|
||||||
|
"client_id": "112924857681130955230",
|
||||||
|
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||||
|
"token_uri": "https://oauth2.googleapis.com/token",
|
||||||
|
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||||
|
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/maxun-test%40maxun-test.iam.gserviceaccount.com",
|
||||||
|
"universe_domain": "googleapis.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user