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: 12
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.
 
Top