Hello,
Use case: every time a deal hits a specific stage, I want to write to a Google Sheet the company name and the link to the deal.
Doing this via custom code as this is the first step of a more complex use case in the future (my issue here is with OAuth).
What I can do: I can console.log a URL to get (manually) an OAuth code, then I edit the value from “const code”. IT WORKS ONCE.
Issue: I need to get an authorization code every single time.
The code quality might not be great right now, but it does write to my Google Sheet file, so I just need to solve the “get a code manually every single time” issue for now. As someone who always uses API Keys, I am not used to OAuth 2.0 authentication.
My code:
const { google } = require('googleapis');
const sheetId = 'XXX'; // The ID to the Google Sheet I want to edit
exports.main = async (event, callback) => {
const contact_company = event.inputFields['contact_company'] || "NO NAME";
const quote_link = event.inputFields['quote_link'];
// These are my credentials from https://console.cloud.google.com/apis/credentials/ - Using OAuth 2.0 type Desktop
const credentials = {
web: {
client_id: 'XXX',
project_id: 'XXX',
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_secret: 'XXX',
redirect_uris: [
'urn:ietf:wg:oauth:2.0:oob',
],
},
};
const oAuth2Client = new google.auth.OAuth2({
clientId: credentials.web.client_id,
clientSecret: credentials.web.client_secret,
redirectUri: 'urn:ietf:wg:oauth:2.0:oob', // Ensure this matches the configured redirect URI
});
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
prompt: 'consent',
scope: ['https://www.googleapis.com/auth/spreadsheets'],
});
// EVERY SINGLE TIME I NEED TO GET A NEW CODE from Google
const code = 'EVERY TIME I NEED TO GET A NEW CODE';
try {
const { tokens } = await oAuth2Client.getToken(code); // This will catch an error, which will give me the URL to get a new code, then I run it again and it works.
oAuth2Client.setCredentials(tokens);
const sheets = google.sheets({
version: 'v4',
auth: oAuth2Client,
});
// Define the function separately without using async in the declaration
const addToGoogleSheet = async (contact_company, quote_link) => {
const sheetName = 'Deals and Links';
const range = `${sheetName}!A:B`;
const response = await sheets.spreadsheets.values.append({
spreadsheetId: sheetId,
range,
valueInputOption: 'USER_ENTERED',
resource: {
values: [[contact_company, quote_link]],
},
});
console.log('Data added to Google Sheet:', response.data);
};
await addToGoogleSheet(contact_company, quote_link);
callback({
outputFields: {
//contact_company: contact_company,
//quote_link: quote_link,
},
});
} catch (error) {
console.log('Authorize this app by visiting this URL:', authUrl);
console.error('Error getting tokens:', error);
}
};
I cannot use an API Key on Google Sheets, it needs to be OAuth.
Anyone had a similar use case and found a solution?
Thank you very much!