1.1.1 • Published 2 years ago
simple-google-client v1.1.1
simple-google-client:
Information:
This is a utility package for projects that interact with some basic Google APIs
- This package is designed to make both authentication and interaction with Google APIs easier
- This package handles the auth flow and token storage to make it easier to use Google APIs
- Your OAuth tokens are encrypted and not stored in plain text
- Changing your
credentials.jsonfile will automatically invalidate previously stored tokens - If possible, your existing OAuth tokens will be refreshed when they are used
Installation:
npm install simple-google-clientUsage:
1. Store your credentials file from your Google Cloud Project as credentials.json in the root directory of your project
- You can get this file by navigating to:
Google Cloud Website -> {Your Project Name} -> APIs & Services -> Credentials -> OAuth 2.0 Client IDs -> {Your Client Name} -> Actions -> Download OAuth Client -> Download JSON - You can manually specify a different path for this file, but I recommend keeping it within your root project directory
2. Require GoogleClient and GoogleAPI from GoogleClient:
const { GoogleClient, GoogleAPI } = require('simple-google-client');3. Make a new instance of GoogleClient:
const client = new GoogleClient();The constructor of GoogleClient takes the following (optional) arguments:
credentialsPath:- Type: String
- Default:
path.join(require.main.path, 'credentials.json') - Info: The path to your configuration file
credentials.jsonfor the OAuth process
scopes:- Type: String Array
- Default:
[] - Info: These are the scopes that the auth process will request via OAuth, a list of which can be found here.
disableLogging- Type: Boolean
- Default:
false - Info: Setting this to true will disable console outputs from
simple-google-client
4. Run GoogleClient.start() on your instance of GoogleClient:
client.start()
.then(async function() {
// Your code here
})
.catch(console.error);GoogleClient.start handles the auth process and returns a promise:
- Resolves if auth succeeds: All code in the
thenblock can then interact withGoogleAPIwithout having to handle any auth - Rejects if auth fails: You will not be able to interact with
GoogleAPIwithout having completed auth - You should only make one call to
GoogleClient.startper instance
Example:
const { GoogleClient, GoogleAPI } = require('simple-google-client');
const client = new GoogleClient(['https://www.googleapis.com/auth/spreadsheets'], false);
client.start()
.then(async function() {
const sheetsAPI = GoogleAPI.sheets('v4');
// This will set: A1=1, B1=2, A2=3, B2=4 (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values#ValueRange)
const sheetValues = [[1,2], [3,4]];
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update
const result = await sheetsAPI.spreadsheets.values.update({
spreadsheetId: 'YourSheetId',
range: 'Sheet1!A1:B2',
valueInputOption: 1,
resource: {
values: sheetValues,
},
});
console.log('Response:', result);
})
.catch(console.error);Disclaimer:
- This project utilizes googleapis and @google-cloud/local-auth, which are third-party dependencies licensed under the Apache 2.0 license.
- This project is not affiliated with or endorsed by Google.
- By using this package, you assume all risks and liabilities associated with its usage.
- The developers and contributors of this project are not responsible for any damages or issues arising from the use of these dependencies.
- It is your responsibility to review and comply with the Apache 2.0 license terms of the dependencies.
⚠️ This project was intentionally designed for a very narrow set of use-cases. I will not be implementing additional features or changes suggested via feedback!