B4A Library Google Authorization with AuthorizationClient

The oauth2 approach implemented here: https://www.b4x.com/android/forum/threads/class-b4x-google-oauth2.79426/#content is no longer supported by Android. Google instead provides a new API for allowing the user to securely authorize access to their account data.

Start by following Google's instructions about the configuration required in Google developer console: https://developer.android.com/identity/authorization#maintain-access

The B4A code is quite simple. There are two steps:
1. Checking whether the user has already granted access.
B4X:
Wait For (ga.AuthorizeMaybeAutomatic(scopes)) Complete (Result As AuthorizationResult)
No UI will be displayed at this point.
Result.ResolutionNeeded tells us whether the authorization flow should be started.

2. If ResultionNeeded is true:
B4X:
Wait For (ga.AuthorizeRequestAccess(Result)) Complete (Result As AuthorizationResult)
At this point the user will be asked to grant access for your app.

Once we have the token we can use it to make requests as demonstrated in the attached example. You will not be able to run the example without making the required configuration.

There is a ClearToken method that clears a cached token. It should be used when a request made with the token fails. Another usage for this is for testing. On the browser, log into your Google account and find the list of connected apps. Remove the app from the list and then call ClearToken to test the full flow again. Note that this method depends on com.google.android.gms:play-services-auth v21.4.0 which isn't installed by default.

Notes:
Manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)

And main module:
B4X:
#AdditionalJar: com.google.android.gms:play-services-auth
 

Attachments

  • GoogleAuthorization.zip
    12.6 KB · Views: 33
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The wrapper code demonstrates several topics that can be relevant for other SDKs:
1. Waiting for async tasks to complete using a do while loop with Sleep. A similar implementation is available in the various ML Kit libraries.
2. More complicated - manipulating B4A "StartActivityForResult" implementation with Activity.startIntentSenderForResult.
 

ZED_App

New Member
hi, Erel, thank you for this great job, i tried it it works correctly for login, but i have not success for logout and clear the token
 

ZED_App

New Member
i find a solution to directly demand a new autorization by:


relogin to google authorization:
Sub logout
    Try
        Dim context As Object = GetContext    ' ✅ هذا السطر ضروري
        Dim Identity As JavaObject
        Identity.InitializeStatic("com.google.android.gms.auth.api.identity.Identity")
        Dim client As JavaObject = Identity.RunMethod("getSignInClient", Array(context))
        client.RunMethod("signOut", Null)
    Catch
        Log(LastException)
    End Try
End Sub
 

fernando1987

Active Member
Licensed User
hi, Erel, thank you for this great job, i tried it it works correctly for login, but i have not success for logout and clear the token
SignOut:
Public Sub SignOut As ResumableSub
    Dim context As JavaObject
    context.InitializeContext

    Dim identity As JavaObject
    identity.InitializeStatic("com.google.android.gms.auth.api.identity.Identity")

    Dim signInClient As JavaObject = identity.RunMethod("getSignInClient", Array(context))
    Dim task As JavaObject = signInClient.RunMethod("signOut", Null)

    Do While Not(task.RunMethod("isComplete", Null).As(Boolean))
        Sleep(50)
    Loop

    Dim success As Boolean = task.RunMethod("isSuccessful", Null).As(Boolean)

   ' If xui.SubExists(mTarget, mEventName & "_SignOutGoogle", 1) Then
        'CallSubDelayed2(mTarget, mEventName & "_SignOutGoogle", success)
   ' End If

    Return success
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
hi, Erel, thank you for this great job, i tried it it works correctly for login, but i have not success for logout and clear the token
Don't confuse authorization with authentication. Use FirebaseAuth for authenticating the user.

The user is not signed in and therefore shouldn't sign out. The user can revoke the token in their Google personal page. There is also a revokeAccess method, though it is designed in such a way that it makes it difficult to use.
 
Top