What is googleapis?
The googleapis npm package is a client library for accessing various Google APIs. It provides an easy way to integrate Google services into applications, allowing developers to interact with a wide range of Google services, including Google Drive, Gmail, Google Calendar, YouTube, and many more.
What are googleapis's main functionalities?
Google Drive API
This code sample demonstrates how to list files in a user's Google Drive using the Google Drive API.
const { google } = require('googleapis');
const drive = google.drive({ version: 'v3', auth });
// List files in Google Drive
drive.files.list({}, (err, res) => {
if (err) throw err;
const files = res.data.files;
if (files.length) {
console.log('Files:');
files.map((file) => {
console.log(`${file.name} (${file.id})`);
});
} else {
console.log('No files found.');
}
});
Gmail API
This code sample shows how to send an email using the Gmail API.
const { google } = require('googleapis');
const gmail = google.gmail({ version: 'v1', auth });
// Send an email using the Gmail API
gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: emailMessage
}
}, (err, res) => {
if (err) throw err;
console.log('Email sent:', res.data);
});
Google Calendar API
This code sample illustrates how to create a new event in a user's Google Calendar.
const { google } = require('googleapis');
const calendar = google.calendar({ version: 'v3', auth });
// Insert a new calendar event
calendar.events.insert({
calendarId: 'primary',
resource: event,
}, (err, event) => {
if (err) throw err;
console.log('Event created: %s', event.htmlLink);
});
YouTube API
This code sample demonstrates how to search for videos on YouTube using the YouTube API.
const { google } = require('googleapis');
const youtube = google.youtube({ version: 'v3', auth });
// Search for videos on YouTube
youtube.search.list({
part: 'snippet',
q: 'Node.js on Google Cloud',
maxResults: 10
}, (err, response) => {
if (err) throw err;
const videos = response.data.items;
if (videos.length) {
console.log('Search results:');
videos.forEach((video) => {
console.log(`${video.snippet.title}`);
});
} else {
console.log('No search results found.');
}
});
Other packages similar to googleapis
aws-sdk
The aws-sdk package is a client library for Amazon Web Services (AWS). It allows developers to interact with a wide range of AWS services such as Amazon S3, EC2, DynamoDB, and more. While it serves a similar purpose for AWS as googleapis does for Google services, the two are for different ecosystems.
#node-googleapis
node-googleapis
allow you to generate client libs for Google APIs.
Installation
npm install googleapis
Usage
var googleapis = require('googleapis);
googleapis.load('urlshortener','v1', function(err, client) {
// set api key
client.setApiKey('AIzaSyAdjHPT5Pb7Nu56WJ_nlrMGOAgUAtKjiPM');
// batch requests
client
.newBatchRequest()
.add('urlshortener.url.get', { shortUrl: 'http://goo.gl/DdUKX' })
.add('urlshortener.url.insert', { longUrl: 'http://burcudogan.com' })
.execute(null, function(errs, results, headers){
});
// a single request
client
.newRequest('urlshortener.url.get', { shortUrl1: 'http://goo.gl/DdUKX' })
.execute(null, function(err, result, headers){
});
// request builders
client.urlshortener.url
.get({ shortUrl: 'http://goo.gl/DdUKX' })
.execute();
});
License
Copyright (c) 2012 Burcu Dogan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.