Using Google Sign-In for Firebase SDK on Android
July 20, 2017 Allen Drennan
July 20, 2017 Allen Drennan
[SHOWTOGROUPS=4,20]
In this article we will show you how to use Google’s Firebase SDK for sign-in on Android devices. While most login implementations launch a web browser to start an OAuth/2 flow even in Windows, iOS and Android apps, we will be demonstrating how to directly use the Google SignIn SDK in your app to present a seamless experience for the user from signing in, all the way to requesting Google authentication scopes and using various Google APIs.
The beauty of this approach is that the experience for the user of your app is smoothly presented. They simply click a button for sign-in with Google and consent to any authentication scopes you want to use.
For more information about us, our support and services visit the Для просмотра ссылки Войдиили Зарегистрируйся or the Для просмотра ссылки Войди или Зарегистрируйся.
The example contained here depends upon part of our Для просмотра ссылки Войдиили Зарегистрируйся.
The source code and related example repository are hosted on GitHub at Для просмотра ссылки Войдиили Зарегистрируйся.
Introduction
If you are building mobile apps for Delphi you are probably already aware of the numerous APIs and services that Google provides for developers. Google APIs range from cloud storage, cloud platform services, cloud messaging, YouTube data access and much more.
Before you start building an app with the Google SDKs and APIs you really need to answer a few questions. Do you intend your app to be cross-platform for mobile and desktop? The approach for Windows applications with Google APIs and mobile apps with Google SDKs is quite different. You will need to address those variances in your implementation. Will Google APIs be called from your client app only or is there a server backend for your app that can make some of the API calls instead? Perhaps you only want to invoke the sign-in process in your mobile app, but all the actual Google API calls occur somewhere else.
Google provides solutions to all of the above scenarios, and we will discuss how to handle these variances in your app implementation, in this article.
Google offers SDKs that you can embed into your iOS and Android applications. The current method that is widely recommended and used in Delphi is to authenticate using OAuth/2 via a web browser. This method requires your user launch a browser to authenticate and internally retrieve an OAuth/2 token. While this works, it is not ideal because users launch the web browser while your app goes into the background and when authentication is completed or fails, must return to your app. Additionally the user is presented with the same behavior on subsequent launches of your application to help verify your authenticating credentials. This is fine if necessary and as a backup method but it is hardly ideal when all you want to present a smoother experience for your user.
Once the user approves your app, future app launches can be completely automatic and silent because the SDK abstracts the complexities of managing the access token and approval process for us.
If you are not interested in the journey to make this all work for Google sign-in, you can skip directly to the sections on the TgoGoogleSDK class and the Getting Started Quick Steps.
Before continuing I wanted to say thank you to Dave Nottage and Для просмотра ссылки Войдиили Зарегистрируйся for their excellent work on Для просмотра ссылки Войди или Зарегистрируйся. Their work provided insight and inspiration for writing this article.
Tokens, tokens, tokens!
Google loves tokens! While most other third-party APIs follow a basic OAuth/2 token flow to secure access, Google uses different tokens for various operations that in Google’s own words, “provides increased security over your standard OAuth 2.0 flow” to their APIs. In their APIs and documentation you will find references to Auth tokens, Id tokens, Access tokens, Refresh tokens, Server auth codes and more. I picture a bunch of bored security researchers sitting around at Google and thinking of new ways to make it complicated for developers to access their APIs in the interest of security. All kidding aside, once you figure out how and where to use the various token methodologies it will all work.
To access the Google APIs in your app from Delphi you first must start by authenticating with Google. Once you are authenticated you can interact with the Google APIs with a variety of approaches. If your application is web based or Windows based, you would obtain an OAuth/2 token from Google and use that token to call the various Google API endpoints using REST/HTTP.
If your application is mobile based on iOS or Android, in the past Google would allow you obtain an OAuth/2 token using the Google SignIn SDK just like you would do on Windows or a web application, so that you could call REST/HTTP APIs directly from your client app. Google however is depreciating this approach on mobile platforms in favor of using Firebase Id tokens instead and obfuscating the process of calling APIs using their SDK along with Firebase Id tokens. On iOS it is still possible to request an OAuth/2 token or a Firebase Id token when you use the Google SignIn SDK, but on Android you can now only obtain a Firebase Id token using the Google SDK. I expect Google to phase out OAuth/2 token access on all mobile platforms in the near future.
If you are developing an app that still wants to call HTTP/REST APIs, perhaps from a backend server process for example, then you still need an OAuth/2 token. Fortunately Google provides a methodology to obtain an OAuth/2 token from your backend. We will also discuss this more later in this article.
If we could easily consume the Google SDKs for Android in Delphi and we didn’t need to make API calls from a server backend, we would not actually care about Google tokens at all. Unfortunately due to some limitations in the JNI marshaling of Delphi, this isn’t entirely possible. I will discuss this more later.
Getting Started with the Google SignIn SDK
To get started with using Google SignIn in your app, you need to take a few steps using the Google Firebase console.
Delphi’s Androidapi.JNI.PlayServices unit
[/SHOWTOGROUPS]
In this article we will show you how to use Google’s Firebase SDK for sign-in on Android devices. While most login implementations launch a web browser to start an OAuth/2 flow even in Windows, iOS and Android apps, we will be demonstrating how to directly use the Google SignIn SDK in your app to present a seamless experience for the user from signing in, all the way to requesting Google authentication scopes and using various Google APIs.
The beauty of this approach is that the experience for the user of your app is smoothly presented. They simply click a button for sign-in with Google and consent to any authentication scopes you want to use.
For more information about us, our support and services visit the Для просмотра ссылки Войди
The example contained here depends upon part of our Для просмотра ссылки Войди
The source code and related example repository are hosted on GitHub at Для просмотра ссылки Войди
Introduction
If you are building mobile apps for Delphi you are probably already aware of the numerous APIs and services that Google provides for developers. Google APIs range from cloud storage, cloud platform services, cloud messaging, YouTube data access and much more.
Before you start building an app with the Google SDKs and APIs you really need to answer a few questions. Do you intend your app to be cross-platform for mobile and desktop? The approach for Windows applications with Google APIs and mobile apps with Google SDKs is quite different. You will need to address those variances in your implementation. Will Google APIs be called from your client app only or is there a server backend for your app that can make some of the API calls instead? Perhaps you only want to invoke the sign-in process in your mobile app, but all the actual Google API calls occur somewhere else.
Google provides solutions to all of the above scenarios, and we will discuss how to handle these variances in your app implementation, in this article.
Google offers SDKs that you can embed into your iOS and Android applications. The current method that is widely recommended and used in Delphi is to authenticate using OAuth/2 via a web browser. This method requires your user launch a browser to authenticate and internally retrieve an OAuth/2 token. While this works, it is not ideal because users launch the web browser while your app goes into the background and when authentication is completed or fails, must return to your app. Additionally the user is presented with the same behavior on subsequent launches of your application to help verify your authenticating credentials. This is fine if necessary and as a backup method but it is hardly ideal when all you want to present a smoother experience for your user.
Once the user approves your app, future app launches can be completely automatic and silent because the SDK abstracts the complexities of managing the access token and approval process for us.
If you are not interested in the journey to make this all work for Google sign-in, you can skip directly to the sections on the TgoGoogleSDK class and the Getting Started Quick Steps.
Before continuing I wanted to say thank you to Dave Nottage and Для просмотра ссылки Войди
Tokens, tokens, tokens!
Google loves tokens! While most other third-party APIs follow a basic OAuth/2 token flow to secure access, Google uses different tokens for various operations that in Google’s own words, “provides increased security over your standard OAuth 2.0 flow” to their APIs. In their APIs and documentation you will find references to Auth tokens, Id tokens, Access tokens, Refresh tokens, Server auth codes and more. I picture a bunch of bored security researchers sitting around at Google and thinking of new ways to make it complicated for developers to access their APIs in the interest of security. All kidding aside, once you figure out how and where to use the various token methodologies it will all work.
To access the Google APIs in your app from Delphi you first must start by authenticating with Google. Once you are authenticated you can interact with the Google APIs with a variety of approaches. If your application is web based or Windows based, you would obtain an OAuth/2 token from Google and use that token to call the various Google API endpoints using REST/HTTP.
If your application is mobile based on iOS or Android, in the past Google would allow you obtain an OAuth/2 token using the Google SignIn SDK just like you would do on Windows or a web application, so that you could call REST/HTTP APIs directly from your client app. Google however is depreciating this approach on mobile platforms in favor of using Firebase Id tokens instead and obfuscating the process of calling APIs using their SDK along with Firebase Id tokens. On iOS it is still possible to request an OAuth/2 token or a Firebase Id token when you use the Google SignIn SDK, but on Android you can now only obtain a Firebase Id token using the Google SDK. I expect Google to phase out OAuth/2 token access on all mobile platforms in the near future.
If you are developing an app that still wants to call HTTP/REST APIs, perhaps from a backend server process for example, then you still need an OAuth/2 token. Fortunately Google provides a methodology to obtain an OAuth/2 token from your backend. We will also discuss this more later in this article.
If we could easily consume the Google SDKs for Android in Delphi and we didn’t need to make API calls from a server backend, we would not actually care about Google tokens at all. Unfortunately due to some limitations in the JNI marshaling of Delphi, this isn’t entirely possible. I will discuss this more later.
Getting Started with the Google SignIn SDK
To get started with using Google SignIn in your app, you need to take a few steps using the Google Firebase console.
- Create a new project at the Firebase Console,
Для просмотра ссылки Войдиили Зарегистрируйся
- Under ‘Project settings’ in the Firebase Console, click ‘Add App’ to add an Android application.
- Choose a package name that matches your Delphi projects Android package name. This is normally something like com.embarcadero.ModuleName
- Add your ‘SHA certificate fingerprint’ by clicking ‘Add Fingerprint’. You will need a SHA fingerprint (also called your SHA1 hash) from every developer’s PC than uses the Google SignIn SDK. If you intend to release your application, you will also need the SHA1 hash from the Release keystore configuration for your specific app. To obtain the correct SHA1 hash value for Delphi Debug configuration applications,
- Locate your debug.keystore file. This is usually under the folder, C:\Users\\AppData\Roaming\Embarcadero\BDS\ on your PC.
- Locate your keytool.exe. This is typically installed with the Java JDK, so if you don’t have the JDK installed somewhere on your PC, you will need it.
- From the command prompt, run:
keytool -list -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android - You will receive an output that includes an SHA1 fingerprint, for example:
Certificate fingerprints:
SHA1: 12 34 56 78 90 12 34 56… - Copy the SHA1 key from the output and paste this value into the ‘Certificate fingerprint’ field in the Firebase console. A quick tip from a command prompt it to hit Ctrl-A, Ctrl-C to copy the output to the clipboard.
> If you need to create a new debug.keystore, you can also do this with the keytool by running,
keytool -genkey -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -validity 14000
- Download the google-services.json from the Firebase console and place this file into your Delphi project folder. For the purposes of Google sign-in and everything we discuss in this article, you do not need to distribute this file with your app. However, it contains several strings that we will need to use in our Delphi project. If you examine the file you will notice there are multiple entries called “client_id”. The one you need for Android is “client_type” 3, also known as the “web client” id if you are using the Google Developers console (Для просмотра ссылки Войди
или Зарегистрируйся) instead of the Firebase console (Для просмотра ссылки Войдиили Зарегистрируйся).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | "oauth_client": [ { "client_id": "<android client id>", "client_type": 1, "android_info": { "package_name": "com.embarcadero.<ModuleName>", "certificate_hash": "329a1a43907c37f4d56e3200f1234567" } }, { "client_id": "<Web client id>", "client_type": 3 } ], |
When you create a project using the Firebase console, Google automatically creates two OAuth/2 Client Ids on your behalf. The first is for the platform (ex: Android or iOS) for the given package name. The second Client Id is called the “Web client” id which is used for API calls to REST/HTTP endpoints. The later is the Client Id we will need for our Android app as well as any HTTP/REST API calls we might make. If you already have an existing and valid “Web client” id in the Google developers console for the project, then the Firebase console will not create a new one.
- Enable the APIs you intend to use in the API Manager of the Google Developers Console. For Google SignIn you will need the Google+ API and probably the Google People API.
Delphi’s Androidapi.JNI.PlayServices unit
This section covers the reasons why you cannot use the Google SDK directly from Delphi, but instead you must create a helper in Java to assist your Delphi application. If you are not interested in the reasons why, you can skip this section.
[/SHOWTOGROUPS]