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 1 commit
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",
});

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>YouTube 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;
});
}
}
24 changes: 24 additions & 0 deletions nodecg-io-googleapis/googleapis-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"clientID": {
"type": "string",
"description": "The oauth client id https://console.cloud.google.com/apis/credentials/oauthclient"
},
"clientSecret": {
"type": "string",
"description": "The oauth client secret https://console.cloud.google.com/apis/credentials/oauthclient"
},
"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."
},
"scope": {
"type": "array",
"description": "Scope URLs for all used services."
}
},
"required": ["clientID", "clientSecret"]
}
50 changes: 50 additions & 0 deletions nodecg-io-googleapis/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "nodecg-io-googleapis",
"version": "0.2.0",
"description": "Allows to connect to and interact with many google-apis",
"homepage": "https://nodecg.io/RELEASE/samples/youtube",
"author": {
"name": "LarsVomMars",
"url": "https://github.com/LarsVomMars"
},
"repository": {
"type": "git",
"url": "https://github.com/codeoverflow-org/nodecg-io.git",
"directory": "nodecg-io-youtube"
},
"files": [
"**/*.js",
"**/*.js.map",
"**/*.d.ts",
"*.json"
],
"main": "extension/index",
"scripts": {
"build": "tsc -b",
"watch": "tsc -b -w",
"clean": "tsc -b --clean"
},
"keywords": [
"nodecg-io",
"nodecg-bundle"
],
"nodecg": {
"compatibleRange": "^1.1.1",
"bundleDependencies": {
"nodecg-io-core": "^0.2.0"
}
},
"license": "MIT",
"devDependencies": {
"@types/node": "^15.0.2",
"nodecg-types": "^1.8.2",
"typescript": "^4.2.4"
},
"dependencies": {
"@types/gapi": "^0.0.39",
"express": "^4.17.1",
"googleapis": "^73.0.0",
"nodecg-io-core": "^0.2.0",
"open": "^8.0.8"
}
}
3 changes: 3 additions & 0 deletions nodecg-io-googleapis/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.common.json"
}