Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

feat(google-slides): add service account authentication (#15648)

Odai Ahmadbf10267a0185
diff --git a/packages/pieces/community/google-slides/package.json b/packages/pieces/community/google-slides/package.json
index f6562ae00bf..1e5609b7251 100644
--- a/packages/pieces/community/google-slides/package.json
+++ b/packages/pieces/community/google-slides/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-google-slides",
- "version": "0.2.0",
+ "version": "0.3.0",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/community/google-slides/src/index.ts b/packages/pieces/community/google-slides/src/index.ts
index ace064d5791..8d26a2a1f90 100644
--- a/packages/pieces/community/google-slides/src/index.ts
+++ b/packages/pieces/community/google-slides/src/index.ts
@@ -1,14 +1,16 @@
-import { createPiece, PieceAuth, OAuth2PropertyValue } from "@activepieces/pieces-framework";
+import { createPiece } from "@activepieces/pieces-framework";
import { createCustomApiCallAction } from '@activepieces/pieces-common';
import { refreshSheetsCharts } from "./lib/actions/refresh-charts";
import { generateFromTemplate } from "./lib/actions/generate-from-template";
import { getPresentation } from "./lib/actions/get-presentation";
-import { googleSlidesAuth } from './lib/auth';
+import { getAccessToken, googleSlidesAuth, GoogleSlidesAuthValue } from './lib/auth';
+
+export { googleSlidesAuth, getAccessToken, GoogleSlidesAuthValue } from './lib/auth';
export const googleSlide = createPiece({
displayName: "Google Slides",
auth: googleSlidesAuth,
- minimumSupportedRelease: '0.36.1',
+ minimumSupportedRelease: '0.86.4',
logoUrl: "https://cdn.activepieces.com/pieces/google-slides.png",
authors: ["Kevinyu-alan"],
actions: [
@@ -19,7 +21,7 @@ export const googleSlide = createPiece({
baseUrl: () => 'https://slides.googleapis.com/v1/presentations/',
auth: googleSlidesAuth,
authMapping: async (auth) => ({
- Authorization: `Bearer ${(auth).access_token}`,
+ Authorization: `Bearer ${await getAccessToken(auth as GoogleSlidesAuthValue)}`,
}),
}),
],
diff --git a/packages/pieces/community/google-slides/src/lib/actions/generate-from-template.ts b/packages/pieces/community/google-slides/src/lib/actions/generate-from-template.ts
index 0c95adfca41..a1f699e1ad8 100644
--- a/packages/pieces/community/google-slides/src/lib/actions/generate-from-template.ts
+++ b/packages/pieces/community/google-slides/src/lib/actions/generate-from-template.ts
@@ -1,8 +1,7 @@
-import { googleSlidesAuth } from '../auth';
+import { createGoogleClient, getAccessToken, googleSlidesAuth, GoogleSlidesAuthValue } from '../auth';
import { createAction, DynamicPropsValue, Property } from "@activepieces/pieces-framework";
import { getSlide, PageElement, batchUpdate, TableCell, TextElement } from '../commons/common';
import { drive as googleDrive } from '@googleapis/drive';
-import { OAuth2Client } from 'google-auth-library';
function extractPlaceholders(content: string, fields: Record<string, any>, placeholder_format: string) {
const regex = placeholder_format === '[[]]'
@@ -62,7 +61,8 @@ export const generateFromTemplate = createAction({
if (!template_presentation_id || !auth)
return {};
- const presentation = await getSlide(auth["access_token"] as unknown as string, template_presentation_id as unknown as string);
+ const accessToken = await getAccessToken(auth as GoogleSlidesAuthValue);
+ const presentation = await getSlide(accessToken, template_presentation_id as unknown as string);
if (!presentation)
return {}
@@ -108,13 +108,11 @@ export const generateFromTemplate = createAction({
})
},
async run(context) {
- const { access_token } = context.auth;
const { template_presentation_id, placeholder_format, table_data } = context.propsValue;
try {
- const authClient = new OAuth2Client();
- authClient.setCredentials({ access_token: access_token });
-
+ const authClient = await createGoogleClient(context.auth);
+
const drive = googleDrive({ version: 'v3', auth: authClient });
const copyResponse = await drive.files.copy({
@@ -149,7 +147,7 @@ export const generateFromTemplate = createAction({
if (requests.length > 0) {
await batchUpdate(
- access_token,
+ await getAccessToken(context.auth),
newPresentationId,
requests
);
diff --git a/packages/pieces/community/google-slides/src/lib/actions/get-presentation.ts b/packages/pieces/community/google-slides/src/lib/actions/get-presentation.ts
index 7406a7108d0..5200fc3b53a 100644
--- a/packages/pieces/community/google-slides/src/lib/actions/get-presentation.ts
+++ b/packages/pieces/community/google-slides/src/lib/actions/get-presentation.ts
@@ -1,4 +1,4 @@
-import { googleSlidesAuth } from '../auth';
+import { getAccessToken, googleSlidesAuth } from '../auth';
import { createAction, Property } from "@activepieces/pieces-framework";
import { getSlide } from "../commons/common";
@@ -19,7 +19,7 @@ export const getPresentation = createAction({
},
async run(context) {
const { presentation_id } = context.propsValue;
- const { access_token } = context.auth;
- return await getSlide(access_token, presentation_id);
+ const accessToken = await getAccessToken(context.auth);
+ return await getSlide(accessToken, presentation_id);
},
});
\ No newline at end of file
diff --git a/packages/pieces/community/google-slides/src/lib/actions/refresh-charts.ts b/packages/pieces/community/google-slides/src/lib/actions/refresh-charts.ts
index bec8278976b..66e85509904 100644
--- a/packages/pieces/community/google-slides/src/lib/actions/refresh-charts.ts
+++ b/packages/pieces/community/google-slides/src/lib/actions/refresh-charts.ts
@@ -1,6 +1,6 @@
import { createAction, Property } from '@activepieces/pieces-framework';
import { batchUpdate, getSlide, PageElement } from '../commons/common';
-import { googleSlidesAuth } from '../auth';
+import { getAccessToken, googleSlidesAuth } from '../auth';
export const refreshSheetsCharts = createAction({
name: 'refresh_sheets_charts',
@@ -19,8 +19,8 @@ export const refreshSheetsCharts = createAction({
},
async run(context) {
const { presentation_id } = context.propsValue;
- const { access_token } = context.auth;
- const presentation = await getSlide(access_token, presentation_id);
+ const accessToken = await getAccessToken(context.auth);
+ const presentation = await getSlide(accessToken, presentation_id);
const requests: { refreshSheetsChart: { objectId: string; }; }[] = [];
@@ -38,7 +38,7 @@ export const refreshSheetsCharts = createAction({
});
if (requests.length > 0) {
- const result = await batchUpdate(access_token, presentation_id, requests);
+ const result = await batchUpdate(accessToken, presentation_id, requests);
return {
success: true,
message: `Successfully refreshed ${requests.length} Google Sheets charts`,
diff --git a/packages/pieces/community/google-slides/src/lib/auth.ts b/packages/pieces/community/google-slides/src/lib/auth.ts
index ee471de96e2..fa2ce646748 100644
--- a/packages/pieces/community/google-slides/src/lib/auth.ts
+++ b/packages/pieces/community/google-slides/src/lib/auth.ts
@@ -1,15 +1,104 @@
-import { PieceAuth } from '@activepieces/pieces-framework';
-
-export const googleSlidesAuth = PieceAuth.OAuth2({
- description: '',
-
- authUrl: 'https://accounts.google.com/o/oauth2/auth',
- tokenUrl: 'https://oauth2.googleapis.com/token',
- required: true,
- scope: [
- 'https://www.googleapis.com/auth/presentations',
- 'https://www.googleapis.com/auth/drive',
- 'https://www.googleapis.com/auth/spreadsheets',
- 'email',
- ],
-});
+import {
+ AppConnectionType,
+ AppConnectionValueForAuthProperty,
+ PieceAuth,
+ Property,
+} from '@activepieces/pieces-framework';
+import { JWT, OAuth2Client } from 'google-auth-library';
+
+export const googleSlidesScopes = [
+ 'https://www.googleapis.com/auth/presentations',
+ 'https://www.googleapis.com/auth/drive',
+ 'https://www.googleapis.com/auth/spreadsheets',
+ 'email',
+];
+
+export const googleSlidesAuth = [
+ PieceAuth.OAuth2({
+ description: '',
+
+ authUrl: 'https://accounts.google.com/o/oauth2/auth',
+ tokenUrl: 'https://oauth2.googleapis.com/token',
+ required: true,
+ scope: googleSlidesScopes,
+ }),
+ PieceAuth.CustomAuth({
+ displayName: 'Service Account (Advanced)',
+ description:
+ 'Authenticate via service account from https://console.cloud.google.com/ > IAM & Admin > Service Accounts > Create Service Account > Keys > Add key. <br> <br> You can optionally use domain-wide delegation (https://support.google.com/a/answer/162106?hl=en#zippy=%2Cset-up-domain-wide-delegation-for-a-client) to access presentations without adding the service account to each one. <br> <br> **Note:** Without a user email, the service account only has access to files/folders you explicitly share with it. A service account also has no Drive storage of its own, so Generate from Template, which copies the template into a new file, fails unless domain-wide delegation is configured.',
+ required: true,
+ props: {
+ serviceAccount: Property.LongText({
+ displayName: 'Service Account JSON Key',
+ required: true,
+ }),
+ userEmail: Property.ShortText({
+ displayName: 'User Email',
+ required: false,
+ description:
+ 'Email address of the user to impersonate for domain-wide delegation.',
+ }),
+ },
+ validate: async ({ auth }) => {
+ try {
+ await getAccessToken({
+ type: AppConnectionType.CUSTOM_AUTH,
+ props: { ...auth },
+ });
+ } catch (e) {
+ return {
+ valid: false,
+ error: (e as Error).message,
+ };
+ }
+ return {
+ valid: true,
+ };
+ },
+ }),
+];
+
+export async function createGoogleClient(
+ auth: GoogleSlidesAuthValue
+): Promise<OAuth2Client> {
+ if (auth.type === AppConnectionType.CUSTOM_AUTH) {
+ let serviceAccount;
+ try {
+ serviceAccount = JSON.parse(auth.props.serviceAccount);
+ } catch {
+ throw new Error(
+ 'Invalid Service Account JSON Key. Please provide a valid JSON string.'
+ );
+ }
+ return new JWT({
+ email: serviceAccount.client_email,
+ key: serviceAccount.private_key,
+ scopes: googleSlidesScopes,
+ subject: auth.props.userEmail,
+ });
+ }
+ const authClient = new OAuth2Client();
+ authClient.setCredentials(auth);
+ return authClient;
+}
+
+export const getAccessToken = async (
+ auth: GoogleSlidesAuthValue
+): Promise<string> => {
+ if (auth.type === AppConnectionType.CUSTOM_AUTH) {
+ const googleClient = await createGoogleClient(auth);
+ const response = await googleClient.getAccessToken();
+ if (response.token) {
+ return response.token;
+ } else {
+ throw new Error(
+ 'Could not retrieve access token from service account json'
+ );
+ }
+ }
+ return auth.access_token;
+};
+
+export type GoogleSlidesAuthValue = AppConnectionValueForAuthProperty<
+ typeof googleSlidesAuth
+>;