@microbackend/plugin-googleapis
v1.0.1
Published
Micro-backend googleapis plugin
Downloads
9
Readme
@microbackend/plugin-googleapis
Microbackend plugin to add support for googleapis.
Installation
npx microbackend plugin add @microbackend/plugin-googleapis
Usage
import createPluginConfig from "@microbackend/plugin-core/build/webpack.build";
await createPluginConfig({
pluginOptions: {
"@microbackend/plugin-googleapis": {
credentialsEnvVariable: "CUSTOM_GOOGLE_OAUTH2_CREDENTIALS",
credentialsPath: path.resolve(
"credential",
"google_oauth_credentials.json"
),
tokenEnvVariable: "CUSTOM_GOOGLE_OAUTH2_TOKEN",
tokenPath: path.resolve("credential", "google_oauth_token.json"),
},
},
});
If not provided:
credentialsEnvVariable
defaults toGOOGLE_OAUTH2_CREDENTIALS
.tokenEnvVariable
defaults toGOOGLE_OAUTH2_TOKEN
.
The above configuration does the following steps:
- Read the Google OAuth2 credentials file specified by
credentialsPath
. - Store the OAuth2 credentials in the environment variable specified by
credentialsEnvVariable
, which can then be accessed during runtime like this:process.env["CUSTOM_GOOGLE_OAUTH2_CREDENTIALS"]
. - Read the Google OAuth2 token file specified by
tokenPath
. - Store the token contents in the environment variable specified by
tokenEnvVariable
, which can then be accessed during runtime like this:process.env["CUSTOM_GOOGLE_OAUTH2_TOKEN"]
.
The plugin creates an extension on app
to expose a googleOAuth2Client
property using the credentials and token.
For more information on how to use the googleapis Node.js SDK, please read its documentation.
Acquiring a Google OAuth2 token
In order to construct the Google OAuth2 client, we need to first acquire a Google OAuth2 token, like so:
// @ts-check
const {
authorizeGoogle,
} = require("@haipham/javascript-helper-google-build-utils");
(async () => {
await authorizeGoogle({
credentialPath: {{credentialsPath}},
tokenPath: {{tokenPath}},
scopes: [
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.send",
],
});
})();