B4A Library GoogleIdentity - Sign in with Google

Erel

Administrator
Staff member
Licensed User
Edit: It is recommended to use FirebaseAuth for new projects:
FirebaseAuth - Authenticate your users


This library wraps Google Identity Toolkit SDK: https://developers.google.com/identity/toolkit/



Currently it only supports signing with Google accounts.

Using this library requires some configuration.

1. Carefully follow the instructions under Step 2: https://developers.google.com/identity/toolkit/android/quickstart#step_2_configure_the_google_identity_toolkit_api

You can find the sha1 signature in the private sign key dialog:



2. Add the following code to the manifest editor:
B4X:
AddApplicationText(
    <meta-data
  android:name="com.google.android.gms.version"
  android:value="@integer/google_play_services_version" />
  <meta-data
  android:name="identitytoolkit.show_providers"
  android:value="Google" />
  <meta-data
  android:name="identitytoolkit.use_google_plus"
  android:value="false" />
  <meta-data
  android:name="identitytoolkit.api_key"
  android:value="***  API keys Type = Android ***" />
  <meta-data
  android:name="identitytoolkit.server_client_id"
  android:value="*** OAuth 2.0 client IDs Type = Web application ***" />
  <meta-data
  android:name="identitytoolkit.server_widget_url"
  android:value="/" />
)
You need to replace the two marked keys.

3. Add the following attributes:
B4X:
#AdditionalRes: $AndroidSDK$\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
#AdditionalRes: $AdditionalLibs$\extra-res\identity, com.google.identitytoolkit
#ExcludeClasses: .games, .drive, .fitness, .wearable, .measurement, .cast, .nearby, .maps
#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .vision, .gcm
#ExcludeClasses: .ads
The code itself is simple:
B4X:
Sub Process_Globals

End Sub

Sub Globals
   Private gi As IdentityClient
   Private lblUsername As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   gi.Initialize("gi")
End Sub

   Sub btnSignin_Click
   gi.StartSignin
End Sub

Sub gi_Signin (Success As Boolean, User As IdentityUser)
   If Success Then
     Log($"Name: ${User.DisplayName}"$)
     Log($"Email: ${User.Email}"$)
     Log($"Photo: ${User.PhotoUrl}"$)
     lblUsername.Text = $"Username: ${User.DisplayName}"$
   Else
     Log("Error")
   End If
End Sub

The library is attached. Make sure to copy all files including the folder to the additional libraries folder.
 

Attachments

Last edited:

MarcoRome

Expert
Licensed User
Already Try and work very well.
Erel do you think that you will add support also for FaceBook, Papypal , etc?
Thank you
Marco
 

Erel

Administrator
Staff member
Licensed User
You need to follow the steps in the linked tutorial.
 

b4auser1

Well-Known Member
Licensed User
You need to follow the steps in the linked tutorial.
I read the tutorial and investigated example and I didn't find how to obtaint from GoogleIdentity lib <access token> to work (f.e. with Google Drive API).
Please explain how to do it.
 

luke2012

Well-Known Member
Licensed User
Thanks for this useful library @Erel!
Using this library can I set the SMTP user credentials (GMAIL) in order to send a background eMail (for example when a user click on a botton) ?
 

b4auser1

Well-Known Member
Licensed User
Using this library can I set the SMTP user credentials (GMAIL) in order to send a background eMail (for example when a user click on a botton) ?
There is SMTP protocol and you can use
http://www.b4x.com/android/help/net.html#smtp
to send email. You don't need for this GoogleIdentity library. Please don't forget turn on https://support.google.com/accounts/answer/6010255?hl=en

Another option is to use Google GMail API. To use this API you need to obtain <OAuth 2.0 access token>. I have already asked how to obtain <OAuth 2.0 access token> from GoogleIdentity lib in my post above.
Now I use either
this approach https://www.b4x.com/android/forum/threads/oauth-2-0-google-web-services-tutorial.14517/
or this one https://www.b4x.com/android/forum/threads/googleaccountmanager-googlesignin.63961/
to obtain <OAuth 2.0 access token>.
 

Erel

Administrator
Staff member
Licensed User
Using this library can I set the SMTP user credentials (GMAIL) in order to send a background eMail (for example when a user click on a botton) ?
No.

I read the tutorial and investigated example and I didn't find how to obtaint from GoogleIdentity lib <access token> to work (f.e. with Google Drive API).
There is no access token. This library does one thing. It allows the user to sign in with his Google account. In the future it will allow signing in with different accounts.

You can use it to authenticate the user.
 

adrianstanescu85

Active Member
Licensed User
I just tried to set up the credentials and copied the two keys to the manifest editor. I tried having them work with the included example in this post, but with no success, I select my email address from the list and... nothing happens.

I set up first the API Key to "AIzaSyAMzO70tgmn5Ks56fQzI-zIj_EH0vkJRvg" and the OAuth 2.0 client IDs Web application to "42755122809-496leqpt5s1rrh2kabbqmvb4vvcn7r1n.apps.googleusercontent.com".

The API is enabled and I've set the rest of the settings as written in the tutorial.

Anyone having any idea why this wouldn't work?

Thank you!
 

MarcoRome

Expert
Licensed User
Hi Adrian look this screen:
You insert and modified Manifest file, where point 2 in manifest is your KEY, point 1 value is SERVER CLIENT ), and choose name package.

1app.png

2app.png

This value you can take in your Developers Console, follow istrction Erel and look this pictures:
This is your KEY ( point 2 manifest )

5app.png

and this is SERVER CLIENT ( Point 1 in manifest )


3app.png

In "Android Client" you insert your SHA1 ( As already explain Erel ) + name package as in this picture:

4app.png

Thats all
 

adrianstanescu85

Active Member
Licensed User
Hi Marco,

Thank you for pointing out the steps! That was exactly what I done so far.

However, I went over everything one more time.




Still no effect, and I'm attaching the screenshot.

Any idea?

Adrian
 

MarcoRome

Expert
Licensed User
1. Can you make a Print screen "Android client 1" ?
2. When you click about Button for connect with Google as in example Erel, what you have as error ?
 

adrianstanescu85

Active Member
Licensed User
Yes, here it is:

And here is the log:
B4X:
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.identity
Copying updated assets files (2)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
 

MarcoRome

Expert
Licensed User
Is right also this.
Do you do this last step:

You're almost there! You just need to decide which sign-in options to support.
  1. On the left-side menu, return to the APIs list by selecting API Manager.
  2. Select the Enabled APIs tab.
  3. From the list of enabled APIs, find the Identity Toolkit API, then select the gear icon to the right of the name. This action opens your sign-in page configuration.
    • In the URL Configuration section of the Identity Toolkit configuration screen, you will need to insert some placeholder values since you aren't yet configuring a web server.
      • Widget URL
        From the drop-down, select a URL. This is the URL you entered earlier in the Client ID for Web application, under "Authorized Redirect URIs". For this quick-start app, set this to: /callback
      • Sign-in Success URL
        For this quick-start app, set this to: /
      • Sign-out URL
        For this quick-start app, set this to: /
      • Send Email URL
        For this quick-start app, set this to: /
 
Top