Readme

Pipedream helpers

Pipedream offers an Accounts API to handle OAuth for you, automatically, and for free. How to do it is covered in this guide.

The helper functions below can make it easier to work with various Google API. For example, I used them like so to get all my free/busy times for this week from all my google calendars:

import { pipeDreamGoogle } from "https://esm.town/v/stevekrouse/pipedream"; const accountId = "REPLACE_WITH_YOUR_GCAL_ACCOUNT_ID_FROM_PIPEDREAM"; const calendar = await pipeDreamGoogle("calendar", accountId); // Function to get free/busy information async function getFreeBusyTimes(calendarId: string) { const now = new Date(); const startOfWeek = new Date(now); startOfWeek.setDate(now.getDate() - now.getDay()); const endOfWeek = new Date(startOfWeek); endOfWeek.setDate(startOfWeek.getDate() + 6); const requestBody = { timeMin: startOfWeek.toISOString(), timeMax: endOfWeek.toISOString(), items: [{ id: calendarId }], }; const freeBusyResponse = await calendar.freebusy.query({ requestBody, }); return freeBusyResponse.data.calendars[calendarId].busy; } const calendars = (await calendar.calendarList.list()).data.items; const freeBusy = await Promise.all(calendars.map(calendar => getFreeBusyTimes(calendar.id))); console.log(freeBusy);

In order for this to work you need to follow the instructions in this guide to save your pipedream API key into your Val Town Environment Variables under pipedream, then connect the google account you care about to Pipedream, and copy it's Pipedream "account id" into your val code. Let me know if you need any help by commenting on this val!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=44";
import { google } from "npm:googleapis";
export async function getAccessToken(accountId: string, bearer = Deno.env.get("pipedream")) {
const response = await fetchJSON(
`https://api.pipedream.com/v1/accounts/${accountId}?include_credentials=1`,
{ bearer },
);
return response.data.credentials.oauth_access_token;
}
export function googleService(service: string, accessToken: string) {
return google[service]({
version: "v3",
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
}
export async function pipeDreamGoogle(service: string, accountId: string, bearer = Deno.env.get("pipedream")) {
const accessToken = await getAccessToken(accountId, bearer);
return googleService(service, accessToken);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 17, 2024