with Google Identity and MongoDB?
Create a new document in a collection of your choice. See the docs here
Execute an aggregation pipeline on a MongoDB collection. See the documentation
import axios from "axios";
import * as jsrsasign from "jsrsasign";
export default defineComponent({
props: {
google_identity: {
type: "app",
app: "google_identity",
}
},
async run({ steps, $ }) {
const serviceAccountKey = JSON.parse(this.google_identity.$auth.service_account_key);
const GOOGLE_OAUTH = 'https://www.googleapis.com/oauth2/v4/token';
// add/remove your own scopes as needed
const SCOPES = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/cloud-identity',
];
const getJwt = async function ({ client_email, private_key }, iat) {
const header = {
typ: 'JWT',
alg: 'RS256',
};
const exp = iat + 3600;
const payload = {
aud: GOOGLE_OAUTH,
iss: client_email,
scope: SCOPES.join(' '),
iat,
exp,
};
const jwt = jsrsasign.KJUR.jws.JWS.sign(null, header, payload, private_key);
return { jwt, exp };
};
const getToken = async function (serviceAccountKey) {
const now = Math.floor(Date.now() / 1000);
const jwtInfo = await getJwt(serviceAccountKey, now);
const params = new URLSearchParams();
params.append('grant_type', 'urn:ietf:params:oauth:grant-type:jwt-bearer');
params.append('assertion', jwtInfo.jwt);
const response = await axios.post(GOOGLE_OAUTH, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const accessToken = response.data.access_token;
return accessToken;
};
const generateOAuthToken = async function () {
return await getToken(serviceAccountKey);
};
// Call the function to generate the token
const accessToken = await generateOAuthToken();
// Call the Google Identity API to search for groups
const response = await axios({
method: "GET",
url: "https://cloudidentity.googleapis.com/v1/groups:search",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json"
},
params: {
query: "parent == 'customers/C046psxkn'"
}
})
return response.data
},
})
The MongoDB API provides powerful capabilities to interact with a MongoDB database, allowing you to perform CRUD (Create, Read, Update, Delete) operations, manage databases, and execute sophisticated queries. With Pipedream, you can harness these abilities to automate tasks, sync data across various apps, and react to events in real-time. It’s a combo that’s particularly potent for managing data workflows, syncing application states, or triggering actions based on changes to your data.
import mongodb from 'mongodb'
export default defineComponent({
props: {
mongodb: {
type: "app",
app: "mongodb",
},
collection: {
type: "string"
},
filter: {
type: "object"
}
},
async run({steps, $}) {
const MongoClient = mongodb.MongoClient
const {
database,
hostname,
username,
password,
} = this.mongodb.$auth
const url = `mongodb+srv://${username}:${password}@${hostname}/test?retryWrites=true&w=majority`
const client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const db = client.db(database)
const results = await db.collection(this.collection).find(this.filter).toArray();
$.export('results', results);
await client.close()
},
})