Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions nodecg-io-googleapis/extension/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { NodeCG } from "nodecg-types/types/server";
import { Result, emptySuccess, success, error, ServiceBundle } from "nodecg-io-core";
import { google, GoogleApis } from "googleapis";
import type { Credentials } from "google-auth-library/build/src/auth/credentials";
import type { OAuth2Client } from "google-auth-library/build/src/auth/oauth2client";
import * as express from "express";
import opn = require("open");

interface GoogleApisServiceConfig {
clientID: string;
clientSecret: string;
refreshToken?: string;
scopes?: string | string[];
}

export type GoogleApisServiceClient = GoogleApis;

module.exports = (nodecg: NodeCG) => {
new GoogleApisService(nodecg, "googleapis", __dirname, "../googleapis-schema.json").register();
};

class GoogleApisService extends ServiceBundle<GoogleApisServiceConfig, GoogleApisServiceClient> {
async validateConfig(_config: GoogleApisServiceConfig): Promise<Result<void>> {
return emptySuccess();
}

async createClient(config: GoogleApisServiceConfig): Promise<Result<GoogleApisServiceClient>> {
const auth = new google.auth.OAuth2({
clientId: config.clientID,
clientSecret: config.clientSecret,
redirectUri: "http://localhost:9090/nodecg-io-googleapis/oauth2callback",
});

await this.refreshTokens(config, auth);

const client = new GoogleApis({ auth });
return success(client);
}

stopClient(_client: GoogleApisServiceClient): void {
return;
}

private async initialAuth(config: GoogleApisServiceConfig, auth: OAuth2Client): Promise<Credentials> {
const authURL = auth.generateAuthUrl({
access_type: "offline",
prompt: "consent",
scope: config.scopes,
});

return new Promise((resolve, reject) => {
const router = express.Router();

router.get("/nodecg-io-googleapis/oauth2callback", async (req, res) => {
try {
const response = `<html><head><script>window.close();</script></head><body>Google Api connection successful! You may close this window now.</body></html>`;
res.send(response);

const { tokens } = await auth.getToken(req.query.code as string);
resolve(tokens);
} catch (e) {
reject(error(e));
}
});

this.nodecg.mount(router);
opn(authURL, { wait: false }).then((cp) => cp.unref());
});
}

private async refreshTokens(config: GoogleApisServiceConfig, auth: OAuth2Client) {
if (config.refreshToken) {
this.nodecg.log.info("Re-using saved refresh token.");
auth.setCredentials({ refresh_token: config.refreshToken });
} else {
this.nodecg.log.info("No refresh token found. Starting auth flow to get one ...");
auth.setCredentials(await this.initialAuth(config, auth));
if (auth.credentials.refresh_token) {
config.refreshToken = auth.credentials.refresh_token;
}
}

auth.on("tokens", (tokens) => {
if (tokens.refresh_token) config.refreshToken = tokens.refresh_token;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"refreshToken": {
"type": "string",
"description": "Token that allows the client to refresh access tokens. This is set automatically after first login, you don't need to set it."
},
"scopes": {
"type": "array",
"description": "Scope URLs for all used services."
}
},
"required": ["clientID", "clientSecret"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "nodecg-io-youtube",
"name": "nodecg-io-googleapis",
"version": "0.2.0",
"description": "Allows to connect and interact to youtube",
"description": "Allows to connect to and interact with many google-apis",
"homepage": "https://nodecg.io/RELEASE/samples/youtube",
"author": {
"name": "CodeOverflow team",
"url": "http://codeoverflow.org"
"name": "LarsVomMars",
"url": "https://github.com/LarsVomMars"
},
"repository": {
"type": "git",
Expand Down
86 changes: 0 additions & 86 deletions nodecg-io-gsheets/extension/index.ts

This file was deleted.

50 changes: 0 additions & 50 deletions nodecg-io-gsheets/package.json

This file was deleted.

91 changes: 0 additions & 91 deletions nodecg-io-youtube/extension/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions nodecg-io-youtube/tsconfig.json

This file was deleted.

20 changes: 0 additions & 20 deletions nodecg-io-youtube/youtube-schema.json

This file was deleted.

11 changes: 6 additions & 5 deletions samples/gsheets/extension/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { NodeCG } from "nodecg-types/types/server";
import { GSheetsServiceClient } from "nodecg-io-gsheets";
import { GoogleApisServiceClient } from "nodecg-io-googleapis";
import { requireService } from "nodecg-io-core";

module.exports = function (nodecg: NodeCG) {
nodecg.log.info("Sample bundle for Google Sheets started");

const gsheets = requireService<GSheetsServiceClient>(nodecg, "gsheets");
const googleApis = requireService<GoogleApisServiceClient>(nodecg, "googleapis");

gsheets?.onAvailable(async (client) => {
googleApis?.onAvailable(async (client) => {
const gsheets = client.sheets("v4");
try {
const data = await client.spreadsheets.values.get(
const data = await gsheets.spreadsheets.values.get(
{
spreadsheetId: "<ID>", //Spreadsheet ID, URL is formatted https://docs.google.de/spreadsheets/d/<ID>/edit
range: "<tableSheetName>", //The sheet name, witch will used to get the data.
Expand All @@ -24,5 +25,5 @@ module.exports = function (nodecg: NodeCG) {
}
});

gsheets?.onUnavailable(() => nodecg.log.info("GSheets client has been unset."));
googleApis?.onUnavailable(() => nodecg.log.info("GSheets client has been unset."));
};
Loading