Skip to content

Google Sign In implementation for Android using Chrome custom tabs (not SDK implementation)

Notifications You must be signed in to change notification settings

megaacheyounes/google-signin-custom-tabs

Repository files navigation

Google OAuth2 Sign-In for Android without Google SDK

Introduction

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.

Contents

How It Works

This project uses the following steps to achieve Google Sign-In without Google SDK:

  1. OAuth 2.0 Authorization: Redirects the user to Google's OAuth 2.0 authorization endpoint using Chrome Custom Tabs.
  2. 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.
  3. Token Exchange: The app receives an authorization code,which will exchange it for an access token using Retrofit.
  4. Fetch User Profile: The access token is used to fetch the user's profile information.

Demo

demo.mp4

video is loading, be patient!

click here if demo fails to load

Project Setup

Go to the Google Developers Console

See Google's official documentation for more details: https://developers.google.com/identity/protocols/oauth2/javascript-implicit-flow

Add Google Credentials to your project

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"

Configure AndroidManifest.xml file

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

License

               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.

About

Google Sign In implementation for Android using Chrome custom tabs (not SDK implementation)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages