This project demonstrates how to implement Google Sign-In on Android devices without using the Google Sign-In SDK. Instead, it leverages OAuth 2.0 Web APIs and Browser Custom Tabs to authenticate users and fetch their profiles.
This approach do not require Google Play services, therefore it should work on all android devices.
This project uses the following steps to achieve Google Sign-In without Google SDK:
- OAuth 2.0 Authorization: Redirects the user to Google's OAuth 2.0 authorization endpoint using Chrome Custom Tabs.
- Redirection: After the user authorizes the app, Google web server then redirects the user to the custom URI scheme of the app, which will launch the app.
- Token Exchange: The app receives an authorization code,which will exchange it for an access token using Retrofit.
- Fetch User Profile: The access token is used to fetch the user's profile information.
demo.mp4
video is loading, be patient!
- Login to google developer console: https://console.cloud.google.com/
- Create a new project or select an existing one.
- create OAuth consent screen: https://console.cloud.google.com/apis/credentials/consentt
- Navigate to the
Credentials
tab: https://console.cloud.google.com/apis/credentialss - Create an OAuth 2.0 Client ID and choose
Web application
as the application type. - Set the authorized redirect URI to a web server you control, e.g.,
https://younes-signin-demo.com/oauth2redirect
. - (optional) for testing, it is recommended to add a test account: go to
credentials
tab, click on your OAuth 2.0 client ID, scroll to test users section, and click onADD USERS
See Google's official documentation for more details: https://developers.google.com/identity/protocols/oauth2/javascript-implicit-flow
In properties.gradle
, add your Google OAuth2.0 credentials as follows (see properties.example.gradle
):
# google oauth sign in
CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"
REDIRECT_URI = "http://your-domain.com/oauth2callback"
Ensure your app can handle Google sign in redirection uri from Chrome Custom tab, by declaring an activity as handler of Google OAuth callback. In AndroidManifest.json
, add an intent filter with your OAuth redirect URI, e.g:
<activity
android:name=".MainActivity"
android:exported="true"
>
<!-- OTHER CODE -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- update the domain below to match the REDIRECT_URI in gradle.properties -->
<data
android:host="your-domain.com"
android:scheme="http" />
</intent-filter>
</activity>
Run and test the demo
DO WHAT YOU WANT TO PUBLIC LICENSE
Copyright (C) 2024 Younes Megaache
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
1. You just DO WHAT YOU WANT TO.