
Product
A Fresh Look for the Socket Dashboard
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
@microfox/google-oauth
Advanced tools
OAuth SDK for google: A robust TypeScript SDK for Google OAuth 2.0 authentication and API integration
A robust TypeScript SDK for Google OAuth 2.0 authentication and API integration. This SDK provides a simple way to integrate Google OAuth 2.0 authentication into your application with built-in security features and TypeScript support.
npm install @microfox/google-oauth
import { GoogleOAuthSdk, GoogleScope } from '@microfox/google-oauth';
const googleOAuthSdk = new GoogleOAuthSdk({
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
redirectUri: 'YOUR_REDIRECT_URI',
scopes: [GoogleScope.OPENID, GoogleScope.PROFILE, GoogleScope.EMAIL], // Optional, these are the default scopes
state: 'custom-state-string', // Optional, a random state will be generated if not provided
});
// The SDK automatically configures offline access and consent prompt
const authUrl = googleOAuthSdk.getAuthUrl();
// This will include access_type=offline and prompt=consent by default
// Get the state from the redirect URL and verify it
const receivedState = new URL(redirectUrl).searchParams.get('state');
const expectedState = await googleOAuthSdk.getState();
if (receivedState === expectedState) {
const code = new URL(redirectUrl).searchParams.get('code');
if (code) {
try {
const { accessToken, refreshToken, idToken, expiresIn } =
await googleOAuthSdk.exchangeCodeForTokens(code);
// Store tokens securely and use them for API calls
} catch (error) {
// Handle token exchange error
console.error('Token exchange failed:', error);
}
}
} else {
// Handle invalid state (potential CSRF attack)
console.error('Invalid state parameter');
}
try {
const { accessToken, expiresIn } =
await googleOAuthSdk.refreshAccessToken(refreshToken);
// Update stored access token
} catch (error) {
// Handle refresh token error
console.error('Token refresh failed:', error);
}
try {
const result = await googleOAuthSdk.validateAccessToken(accessToken);
if (result.isValid) {
console.log('Token is valid');
console.log('Expires at:', new Date(result.expiresAt!).toISOString());
console.log('Scopes:', result.scopes);
console.log('Email:', result.email);
} else {
console.error('Token validation failed:', result.error);
}
} catch (error) {
console.error('Validation error:', error);
}
The SDK provides a comprehensive set of Google API scopes through the GoogleScope
enum:
GoogleScope.OPENID
(openid
) - OpenID Connect scopeGoogleScope.PROFILE
(profile
) - User's basic profile informationGoogleScope.EMAIL
(email
) - User's email addressGoogleScope.CALENDAR
(https://www.googleapis.com/auth/calendar
) - Full access to Google CalendarGoogleScope.CALENDAR_READONLY
(https://www.googleapis.com/auth/calendar.readonly
) - Read-only access to calendarsGoogleScope.CALENDAR_EVENTS
(https://www.googleapis.com/auth/calendar.events
) - Manage calendar eventsGoogleScope.CALENDAR_EVENTS_READONLY
(https://www.googleapis.com/auth/calendar.events.readonly
) - Read-only access to calendar eventsGoogleScope.DRIVE
(https://www.googleapis.com/auth/drive
) - Full access to Google DriveGoogleScope.DRIVE_READONLY
(https://www.googleapis.com/auth/drive.readonly
) - Read-only access to filesGoogleScope.DRIVE_FILE
(https://www.googleapis.com/auth/drive.file
) - Access to files created by the appGoogleScope.DRIVE_METADATA
(https://www.googleapis.com/auth/drive.metadata
) - View and manage metadataGoogleScope.DRIVE_APPDATA
(https://www.googleapis.com/auth/drive.appdata
) - Access to application data folderGoogleScope.DRIVE_SCRIPTS
(https://www.googleapis.com/auth/drive.scripts
) - Modify and execute Google Apps ScriptsGoogleScope.GMAIL
(https://www.googleapis.com/auth/gmail.readonly
) - Read-only access to GmailGoogleScope.GMAIL_SEND
(https://www.googleapis.com/auth/gmail.send
) - Send emails onlyGoogleScope.GMAIL_COMPOSE
(https://www.googleapis.com/auth/gmail.compose
) - Create and modify email draftsGoogleScope.GMAIL_MODIFY
(https://www.googleapis.com/auth/gmail.modify
) - All read/write operations except deleteGoogleScope.GMAIL_FULL
(https://www.googleapis.com/auth/gmail.full
) - Full access to Gmail accountGoogleScope.CONTACTS
(https://www.googleapis.com/auth/contacts
) - Manage contactsGoogleScope.CONTACTS_READONLY
(https://www.googleapis.com/auth/contacts.readonly
) - Read-only access to contactsGoogleScope.CONTACTS_OTHER_READONLY
(https://www.googleapis.com/auth/contacts.other.readonly
) - Read-only access to domain's contactsGoogleScope.YOUTUBE
(https://www.googleapis.com/auth/youtube
) - Manage YouTube accountGoogleScope.YOUTUBE_READONLY
(https://www.googleapis.com/auth/youtube.readonly
) - Read-only access to YouTube dataGoogleScope.YOUTUBE_UPLOAD
(https://www.googleapis.com/auth/youtube.upload
) - Upload YouTube videosGoogleScope.YOUTUBE_PARTNER
(https://www.googleapis.com/auth/youtubepartner
) - Manage YouTube content and channelGoogleScope.PHOTOS
(https://www.googleapis.com/auth/photoslibrary
) - Access to Google Photos libraryGoogleScope.PHOTOS_READONLY
(https://www.googleapis.com/auth/photoslibrary.readonly
) - Read-only access to photosGoogleScope.PHOTOS_SHARING
(https://www.googleapis.com/auth/photoslibrary.sharing
) - Share photos and albumsGoogleScope.FITNESS
(https://www.googleapis.com/auth/fitness.activity.read
) - Read fitness activity dataGoogleScope.FITNESS_ACTIVITY_WRITE
(https://www.googleapis.com/auth/fitness.activity.write
) - Write fitness activity dataGoogleScope.FITNESS_LOCATION_READ
(https://www.googleapis.com/auth/fitness.location.read
) - Read location dataGoogleScope.FITNESS_LOCATION_WRITE
(https://www.googleapis.com/auth/fitness.location.write
) - Write location dataGoogleScope.TASKS
(https://www.googleapis.com/auth/tasks
) - Manage tasks and task listsGoogleScope.TASKS_READONLY
(https://www.googleapis.com/auth/tasks.readonly
) - Read-only access to tasksGoogleScope.SHEETS
(https://www.googleapis.com/auth/spreadsheets
) - Full access to Google SheetsGoogleScope.SHEETS_READONLY
(https://www.googleapis.com/auth/spreadsheets.readonly
) - Read-only access to SheetsGoogleScope.DOCS
(https://www.googleapis.com/auth/documents
) - Full access to Google DocsGoogleScope.DOCS_READONLY
(https://www.googleapis.com/auth/documents.readonly
) - Read-only access to DocsGoogleScope.CLOUD_PLATFORM
(https://www.googleapis.com/auth/cloud-platform
) - Full access to Google Cloud servicesGoogleScope.CLOUD_PLATFORM_READONLY
(https://www.googleapis.com/auth/cloud-platform.read-only
) - Read-only access to Cloud servicesGoogleScope.ANALYTICS
(https://www.googleapis.com/auth/analytics
) - Full access to Analytics dataGoogleScope.ANALYTICS_READONLY
(https://www.googleapis.com/auth/analytics.readonly
) - Read-only access to AnalyticsGoogleScope.CLASSROOM_COURSES
(https://www.googleapis.com/auth/classroom.courses
) - Manage Classroom coursesGoogleScope.CLASSROOM_COURSES_READONLY
(https://www.googleapis.com/auth/classroom.courses.readonly
) - View Classroom coursesGoogleScope.CLASSROOM_ROSTERS
(https://www.googleapis.com/auth/classroom.rosters
) - Manage class rostersGoogleScope.CLASSROOM_PROFILE_EMAILS
(https://www.googleapis.com/auth/classroom.profile.emails
) - View student/teacher email addressesGoogleScope.MEET
(https://www.googleapis.com/auth/meetings.space.readonly
) - View Meet space informationGoogleScope.MEET_ROOMS
(https://www.googleapis.com/auth/meetings.room.readonly
) - View Meet room informationGoogleScope.PEOPLE
(https://www.googleapis.com/auth/people
) - Manage contacts and other people dataGoogleScope.PEOPLE_READONLY
(https://www.googleapis.com/auth/people.readonly
) - Read-only access to people dataGoogleScope.CHAT
(https://www.googleapis.com/auth/chat.messages
) - Allows the application to send and manage chat messages on behalf of the user.GoogleScope.CHAT_READONLY
(https://www.googleapis.com/auth/chat.messages.readonly
) - Provides the application with read-only access to the user's chat messages.GoogleScope.WEBMASTERS
(https://www.googleapis.com/auth/webmasters
) - Grants the application full access to the user's Search Console data.GoogleScope.WEBMASTERS_READONLY
(https://www.googleapis.com/auth/webmasters.readonly
) - Provides the application with read-only access to the user's Search Console data.The SDK uses Zod for input validation and provides clear error messages:
try {
const { accessToken, refreshToken, idToken, expiresIn } =
await googleOAuthSdk.exchangeCodeForTokens(code);
} catch (error) {
if (error instanceof Error) {
console.error('OAuth error:', error.message);
// Error messages will be in format: "error_code: error_description"
}
}
This SDK implements several security features:
Best practices for implementation:
This SDK is released under the MIT License.
FAQs
OAuth SDK for google: A robust TypeScript SDK for Google OAuth 2.0 authentication and API integration
The npm package @microfox/google-oauth receives a total of 134 weekly downloads. As such, @microfox/google-oauth popularity was classified as not popular.
We found that @microfox/google-oauth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
Industry Insights
Terry O’Daniel, Head of Security at Amplitude, shares insights on building high-impact security teams, aligning with engineering, and why AI gives defenders a fighting chance.
Security News
MCP spec updated with structured tool output, stronger OAuth 2.1 security, resource indicators, and protocol cleanups for safer, more reliable AI workflows.