Skip to main content

OAuth2-Google-Authentication-Intergration

OAuth2-Google-Authentication-Intergration

Install required packages

pip install google-auth-oauthlib google-auth
!pip install google-auth-oauthlib google-auth

Create a Google OAuth Client

  • Choose Desktop App

  • Download the JSON file (e.g., client_secret.json)

  • Put the file in your notebook directory. I have already renamed client_secret.json

Minimal Working Code

This code:

  • Opens Google login in your browser

  • User logs in

  • Google redirects back to a local server

  • Code retrieves the tokens

from google_auth_oauthlib.flow import InstalledAppFlow

# The scope indicates what info we want (email + profile)
# SCOPES = ["openid", "email", "profile"]


SCOPES = [
"openid",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
]


# Path to your OAuth 2.0 client ID JSON
CLIENT_SECRET_FILE = "client_secret.json"

# Create a flow instance
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_SECRET_FILE, SCOPES
)

# Run local server — this opens the Google login page
credentials = flow.run_local_server(port=8080)

# Show token info
print("Access Token:", credentials.token)
print("ID Token:", credentials.id_token)
print("Refresh Token:", credentials.refresh_token)
print("User logged in successfully!")

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=661652986182-leoh8069397oikhodkdil8r5gumklq08.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&state=Mb1yboPJip1HMNyT63e1AmtDdDkZ0P&access_type=offline
Access Token: ya29.a0ATi6K2sopTZVcdhcyTnvT91UzY6lP2aC-Qf3LuhDNV4auCKjLuTERkFGMAU5UahbAJ6Ej5mtyvlHFXKtGbCQstbx2aBao_mgpWcpNG3MZSemqeKKzP7Lk45FIJa0kfwM22YbOdgYMvE-oo0pCfYCG6BjYKOOgQ0tM_EWACq1rOQjQD5Eh5PRJMXL6OS9yBbKo6c2RGYaCgYKAfESARMSFQHGX2Mi3Qb5mixWsEeS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eDR9d5RnNICKWZoKcZoKckmb9UFAisIQjXo9nF0zKUS63qBMGJfKfkf1-lAjRNTk1QTU_CzCufQA0MDbsXiY0uLJpIRF5mnRoOuvl9FOpEv3fQ
Refresh Token: 1//0ened5Tp9ojqVCgYIARAAGA4SNwF-L9IrHZTVSab7qyLPPiofCRny-9ZhBzxY2IMzK6Atnk3sCB3mk2EJQ10yPEyQCzpPm_tMw7w
User logged in successfully!
credentials
<google.oauth2.credentials.Credentials at 0x10a23ac90>

Extract User Info (optional)

from google.oauth2 import id_token
from google.auth.transport import requests

request = requests.Request()

user_info = id_token.verify_oauth2_token(
credentials.id_token,
request,
audience=flow.client_config['client_id']
)

user_info

{'iss': 'https://accounts.google.com',
'azp': '661652986182-leoh8069397oikhodkdil8r5gumklq08.apps.googleusercontent.com',
'aud': '661652986182-leoh8069397oikhodkdil8r5gumklq08.apps.googleusercontent.com',
'sub': '108674484068895490099',
'email': 'hotronghai2@gmail.com',
'email_verified': True,
'at_hash': 'hi4MqYZaVWPCMu79VZ8j2Q',
'name': 'Hai Ho Trong',
'picture': 'https://lh3.googleusercontent.com/a/ACg8ocLYLYU_kbnVNQyJFn129kzIWwLZXTsPO81xH4UULjRGIegcuw=s96-c',
'given_name': 'Hai',
'family_name': 'Ho Trong',
'iat': 1764746446,
'exp': 1764750046}