als-google-auth
v1.0.0
Published
Simple Google OAuth2 authorization for Node.js applications
Readme
als-google-auth
Create a project in Google Cloud Console
- Go to Google Cloud Console.
- Sign in to your Google account.
- Click on the project selection icon at the top of the page and select New Project.
- Enter a name for your project (e.g.,
Calendar API Project). - Click Create.
Enable the service you need
- In Google Cloud Console, after creating the project, navigate to API & Services > Library.
- In the search bar, type the service you need, for example, "Google People API," and select it from the search results.
- Click the Enable button to enable the API for your project.
Configure OAuth consent screen
- Go to API & Services > OAuth consent screen.
- User type: Select External.
- In the App Name section, enter any name (e.g.,
Calendar API App). - Add users - Add your email.
- Fill out the other required fields and click Save and Continue.
- Add or remove scopes.
- For example,
https://www.googleapis.com/auth/calendarfor full access.
- For example,
Create credentials
- Go to API & Services > Credentials.
- Click the Create Credentials button and select OAuth Client ID.
- Application type: Select Web application.
- Name the application (e.g.,
Calendar API App). - In Authorized redirect URIs, add
http://localhost:3000. - Click Create.
- Save the
client_idandclient_secret.
Run the app with the following code:
const authorize = require('als-google-auth')
const { google } = require('googleapis');
const tokenPath = './token.json'; // File path to save token
authorize({
port: 3000,
client_id, // Client ID from Google
client_secret, // Client Secret from Google
tokenPath,
scope: ['https://www.googleapis.com/auth/contacts'], // Scopes to work with, registered in Google
}).then(async auth => {
// Example to work with Google Contacts API
const service = google.people({ version: 'v1', auth });
const response = await service.people.connections.list({
resourceName: 'people/me',
personFields: 'names,phoneNumbers,emailAddresses,metadata',
});
console.log(response.data.connections)
})