This repository was archived by the owner on Apr 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
nodecg-io-googleapis - closes #226 #252
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>`; | ||
LarsVomMars marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": { | ||
LarsVomMars marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "type": "array", | ||
| "description": "Scope URLs for all used services." | ||
| } | ||
| }, | ||
| "required": ["clientID", "clientSecret"] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "extends": "../tsconfig.common.json" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.