Google OAuth: Basic Steps
Title: Google OAuth: Basic Steps
Subtitle: A Step-by-Step Guide to Implementing Google OAuth
What is OAuth?
o OAuth (Open Authorization) is a standard protocol for secure
authentication and authorization.
o Google OAuth allows applications to authenticate users and access
Google services securely.
Step 1 - Create a Google Cloud Project
Go to Google Cloud Console
Create a new project or select an existing one.
Navigate to APIs & Services > Credentials.
Click Create Credentials > OAuth 2.0 Client ID.
Step 2 - Configure OAuth Consent Screen
Set up the OAuth consent screen:
o Application Name
o Scopes (permissions requested)
o Authorized Domains
Choose User Type: Internal (GSuite users) or External (public apps).
Step 3 - Generate OAuth Credentials
In Credentials, click Create Credentials > OAuth client ID.
Select Application Type:
o Web Application (for browser-based apps).
o Desktop App (for standalone apps).
Set Authorized Redirect URIs (e.g.,
http://localhost:3000/auth/callback).
Click Create, then copy Client ID & Client Secret.
Step 4 - Implement OAuth Flow
Redirect users to Google’s authorization URL:
https://accounts.google.com/o/oauth2/auth
?client_id=YOUR_CLIENT_ID
&redirect_uri=YOUR_REDIRECT_URI
&response_type=code
&scope=email profile
&access_type=offline
&prompt=consent
User logs in and grants permission.
Google redirects back with an authorization code.
Step 5 - Exchange Code for Access Token
Send a POST request to Google's token endpoint:
curl -X POST https://oauth2.googleapis.com/token \
-d client_id=YOUR_CLIENT_ID \
-d client_secret=YOUR_CLIENT_SECRET \
-d code=AUTH_CODE \
-d grant_type=authorization_code \
-d redirect_uri=YOUR_REDIRECT_URI
Response includes access token and refresh token.
Step 6 - Use Access Token
Use the access token to access Google APIs:
curl -H "Authorization: Bearer ACCESS_TOKEN" \
https://www.googleapis.com/oauth2/v2/userinfo
Step 7 - Refresh Token (Optional)
Use refresh token to get a new access token:
curl -X POST https://oauth2.googleapis.com/token \
-d client_id=YOUR_CLIENT_ID \
-d client_secret=YOUR_CLIENT_SECRET \
-d refresh_token=YOUR_REFRESH_TOKEN \
-d grant_type=refresh_token
Summary
1. Create a Google Cloud project & enable OAuth.
2. Set up OAuth consent screen & credentials.
3. Redirect users to Google's auth URL.
4. Exchange authorization code for an access token.
5. Use the access token to call Google APIs.
6. Refresh tokens when needed.
Thank You!