B4A Library GoogleIdentity - Sign in with Google

Discussion in 'Additional libraries, classes and official updates' started by Erel, May 1, 2016.

  1. Erel

    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/

    [​IMG]

    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/ident...p_2_configure_the_google_identity_toolkit_api

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

    [​IMG]

    2. Add the following code to the manifest editor:
    Code:
    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:
    Code:
    #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:
    Code:
    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.
     

    Attached Files:

    Last edited: Jun 26, 2016
    jazzzzzzz, Tayfur, JordiCP and 19 others like this.
  2. b4auser1

    b4auser1 Well-Known Member Licensed User

    Great ! Please do wrapper for B4i as well.
     
    Pendrush likes this.
  3. MarcoRome

    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
     
    susu likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    I plan to add support for Facebook.
     
    netkomm, so27, Douglas Farias and 3 others like this.
  5. b4auser1

    b4auser1 Well-Known Member Licensed User

    How to get access token which is required to achieve access to Google API ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You need to follow the steps in the linked tutorial.
     
  7. b4auser1

    b4auser1 Well-Known Member Licensed User

    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.
     
  8. luke2012

    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) ?
     
  9. Douglas Farias

    Douglas Farias Expert Licensed User

    finally, I waited a lot for it.
    Many thx Erel.

    :):):):):):):):):):)
     
  10. b4auser1

    b4auser1 Well-Known Member Licensed User

    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>.
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    No.

    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.
     
    luke2012 and b4auser1 like this.
  12. adrianstanescu85

    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!
     
  13. MarcoRome

    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
     
    DonManfred, inakigarm and Erel like this.
  14. adrianstanescu85

    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.

    [​IMG]
    [​IMG]

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

    Any idea?

    Adrian
     
  15. MarcoRome

    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 ?
     
  16. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    Yes, here it is:
    [​IMG]
    And here is the log:
    Code:
    ** 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 **
     
  17. MarcoRome

    MarcoRome Expert Licensed User

    Is right.
    Can i see details about Web Client ?
     
  18. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    Here it is:
    [​IMG]
     
  19. MarcoRome

    MarcoRome Expert Licensed User

    Is right also this.
    Do you do this last step:

     
  20. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    I did that and it looks like this:
    [​IMG]
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice