Android Question Google Auth over HTTPS

Matrix

Member
Licensed User
Hello
I search a solution to get the Google Auth idToken.

I have tested with postmann this Post Request with email and password
https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=AIzaSyB-B_MYd..........

{
"email":"[email protected]",
"password":"123456",
"returnSecureToken":true
}

The response is than follow:

{
"kind": "identitytoolkit#VerifyPasswordResponse",
"localId": "MW1PwkqVMOdr2WyOfuua58Wpz1R2",
"email": "[email protected]",
"displayName": "",
"idToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImZmNTRmZjM0MTFiZmMwMDJiYTBjZDAwNzA2YmEzYmM4NTBiZWIwMmIifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vYWRkcmlua3MtZGV2ZWxvcGVyIiwiYXVkIjoiYWRkcmlua3MtZGV2ZWxvcGVyIiwiYXV0aF90aW1lIjoxNTMzNTU0MTUyLCJ1c2VyX2lkIjoiTVcxUHdrcVZNT2RyMld5T2Z1dWE1OFdwejFSMiIsInN1YiI6Ik1XMVB3a3FWTU9kcjJXeU9mdXVhNThXcHoxUjIiLCJpYXQiOjE1MzM1NTQxNTIsImV4cCI6MTUzMzU1Nzc1MiwiZW1haWwiOiJ0aEBvZW0tbWF0cml4LmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJ0aEBvZW0tbWF0cml4LmNvbSJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn19.YXWc_FpMi1ZuaTyyb3L_-zmBmch0Vjhi0wlUnMZAyxu5Z6Ou39zzo00TD9KTd9MlsC05OtufYzVB107GpNaxpn00KXipuxIgrMSLYOOYO0RWDJ9K0MW6C65I0AqRgNffjewjbdGlMu6gdUHFJsyZ7i895uXSu-LVjxwcXqyRkO0YXFznp1ayzc5rlThri0kA9IZk0XpFF61trk_qmzWcg0Z06YwASALQ9v4RlgMvEnv-M0RY722m_C1WloAnS9gkYGyxZb6AqopX6Lm6H983MoeMzdg7JUo6UCYgPJJJVXaFNRZWzwoPSuwNFgrpF1223d-lLMDQqb5OxYi49j1-HA",
"registered": true,
"refreshToken": "AGdpqezkT8Nm9NZ6qKu8YAbX96D0cpEyZs3I3Oc9oCgQwGFgFItIqotS7VY8zSyIYtEksWVTacUPA-2awHRyo0V8Fu5F0TsMyhjMD2Jgk__eOg8Q89E2UbjTI_b6R9Ghje8-N89LsHCFntwThAmPLOwuhCsH1TdCU1TL9PdCSHJXl_BzsVmGTaCYw9m14bG8uiGUIHMV3M1d25a58Nm9Ol2lREEl6JeZC5LIXp5h4pGd7aeFIBd6HFY",
"expiresIn": "3600"
}

So can I get the idToken which I need.

In B4A I try to get the value with

job1.Initialize("Job1", Me)
Dim objMap As Map: objMap.Initialize
objMap.Put("email", "[email protected]")
objMap.Put("password", "123456")
Dim objJSon As JSONGenerator: objJSon.Initialize(objMap)
Log(objJSon.ToPrettyString(1))
job1.PostString("https://www.googleapis.com/identity...d?key=AIzaSyB-B_MYdC28AaH2WXP2Z0kOEb61tlGXMcE", objJSon.ToPrettyString(1))

but I get only a bad request response - where is my fault??

I hope someone can help me - this is my first project with b4a
thank you
Timo
 

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png


You may want to check this Class
https://www.b4x.com/android/forum/threads/class-b4x-google-oauth2.79426/#content

Maybe you should directly use Firebase Auth
https://www.b4x.com/android/forum/threads/firebaseauth-authenticate-your-users.67875/#content
 
Upvote 0

Matrix

Member
Licensed User
Thank you Manfred

I Think the best way to contact the firebase database is over this
https://www.b4x.com/android/forum/threads/firebaseauth-authenticate-your-users.67875/#content

But I can not start your project.
When I load the project I get a Mavin error - can not find com.google.firebas/firebase-auth

I use platform SDK 26 - I try it also with 28 the same Problem
Over the Bibliothek is the Modul Firebase-Auth(Version 1.04) enabled
If I disable and after this enable, so I get also the same error

Over the SDK Manager I have installed
- Google Play Services V49
- Google Play Android for SDK 26, 28
- SDK 26 , 28

My B4A version is 8.3

I also tried to reinstall everything in a new Pafd ( Java, Andoid SDK, B4X ... ) - unfortunately without success - something seems to be missing

Thank you for Help
Timo
 
Upvote 0

Matrix

Member
Licensed User
Hi Manfred,
Yes I have started ("my reinstall") with a new blanc Folder!
new Installation Java-> new path d:\java , B4X -> new empty path d:\android_new , SDK Tools -> d:\android_new\tools
B4x Path configuration:
D:\Java\jdk1.8.0_181\bin\javac.exe
D:\Android_new\android-sdk\platforms\android-26\android.jar
D:\Android_new\Basic4android\Libraries

I am not sure what I have to activate everything in sdk manager. Can I activate too much? which leads to problems
What do I need as a minimum

I try a new installation on another computer for test

Timo
 
Upvote 0

Matrix

Member
Licensed User
OK
I have install a new installation in a clear directory - In the SDK manager, I have only activated Google play services additionally
and just I get no maven error ! thank you for this info

Unfortunately I get now after the program start an new error message:


** Service (starter) Start **
** Activity (main) Create, isFirst = true **
FirebaseAuth initialized
main_activity_create (B4A line: 25)
auth.Initialize("auth")
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.firebase.auth.FirebaseAuth.addAuthStateListener(com.google.firebase.auth.FirebaseAuth$AuthStateListener)' on a null object reference
at anywheresoftware.b4a.objects.FirebaseAuthWrapper.Initialize(FirebaseAuthWrapper.java:57)
at b4a.firebase.main._activity_create(main.java:378)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b4a.firebase.main.afterFirstLayout(main.java:104)
at b4a.firebase.main.access$000(main.java:17)
at b4a.firebase.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.firebase.auth.FirebaseAuth.addAuthStateListener(com.google.firebase.auth.FirebaseAuth$AuthStateListener)' on a null object reference


I have setup an Project under Firebase Console -
under project settings I have add an Android APP
Adroid APP Name is the App program name : b4a.firebase
than I regitry the app and download the google-services.json and copy this in the program root

thank you for help
Timo
 
Upvote 0

Matrix

Member
Licensed User
ok - no change, the same error

Erl wrote in your link
The correct solution is:

1. Open B4A Sdk Manager and update all recommended items (including firebase-auth).
-> I have a new Installation - no updates

2. Update FirebaseAuth and FirebaseAnalytics: https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.59340/#post-596101
-> download , unzip and copy in to D:\ANDROID\Basic4android\Libraries ... old Files clered before!

3. If you are using B4A v8.00 or below then you should update FirebaseNotifications: https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.59340/#post-592335
-> No update - I use 8.3!

4. If you are using B4A v8.0+ then you should make sure to use the new CreateResourceFromFile macro feature to add the snippets.
https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/#content
If you are using an older version then you should go over the snippets and copy them to your manifest editor. Some of them were updated.
-> No Update - I use 8.3!

Remember to copy the internal libraries to the internal libraries folder.
Yes - I copied the files in D:\ANDROID\Basic4android\Libraries


After the program start I get the same error in line 25 auth.Initalize("auth")
 
Upvote 0

Matrix

Member
Licensed User
Firebasa Analytics.jar is updated! V1.02 from here -> https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.59340/#post-596101

But I haven't edit the Manifest!

what or where do I have to edit something?
I thought these are fixed values or pass variables

here the original Manifest from your demo project which I use:

My target SDK is 27 - I see in the Manifest targetsdkversion="19" ?? If I change this, so I get a parser error - is this correct with 19?



'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
'************ Google Play Services Base ************
AddApplicationText(
<activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="false"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************

'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />)
AddApplicationText(
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<provider
android:authorities="${applicationId}.firebaseinitprovider"
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:exported="false"
android:initOrder="100" />
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="false" />


<service
android:name="com.google.firebase.iid.FirebaseInstanceIdService"
android:exported="true">
<intent-filter android:priority="-500">
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
)
'************ Firebase Base (end) ************
'************ Firebase Auth ************
AddApplicationText(
<activity android:name="com.google.android.gms.auth.api.signin.internal.SignInHubActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:excludeFromRecents="true"
android:exported="false" />

<service
android:name="com.google.android.gms.auth.api.signin.RevocationBoundService"
android:exported="true"
android:permission="com.google.android.gms.auth.api.signin.permission.REVOCATION_NOTIFICATION" />
)
'************ Firebase Auth (end) ************
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Matrix

Member
Licensed User
hello,
I have started a new project and copied the Snippets code.
and activate the library firebaseauth

the manifest is follow:


##############

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

###############

I don't know what I must change ?
-> https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/#content

I thought, b4x creates the new entries automatically - maybe I have to exchange new commands then
 
Upvote 0

Matrix

Member
Licensed User
Thank you Erel,
Now I do not get a program error anymore - perfect!
But the auth.CurrentUser.IsInitialized is false

So I think I have an authentication problem - but I do not know what is the problem
I followed your instructions

1. Register with Firebase and create a new project
2. Add Adroid app under project setting
3. the package name matches my app's package name -> b4a.firebase
4. Create a private Key with B4A / Tools/Private-Key and copy the SHA1 Key
5. Use the SHA1 Key for Fingerprint to register the APP ät Firebase
6. Download the google-services.json in the b4a project root



Region Project Attributes
#ApplicationLabel: B4A Firebase Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False

#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
Private auth As FirebaseAuth
End Sub

Sub Globals
Private lblName As Label
Private btnSignIn As Button
Private btnSignOut As Button
End Sub





Sub Activity_Create(FirstTime As Boolean)

If FirstTime Then
auth.Initialize("auth")
End If

Activity.LoadLayout("layout1")

If auth.CurrentUser.IsInitialized Then
Auth_SignedIn(auth.CurrentUser)
Else
Msgbox("Verbindungsfehler " ,"Firebase")
End If

End Sub

Sub btnSignIn_Click
auth.SignInWithGoogle
End Sub

Sub btnSignOut_Click
auth.SignOutFromGoogle
lblName.Text = "Goodbye!"
End Sub

Sub Auth_SignedIn (User As FirebaseUser)
Log("SignedIn: " & User.DisplayName)
lblName.Text = "Hello: " & User.DisplayName
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

Attachments

  • Project_config.JPG
    Project_config.JPG
    25.2 KB · Views: 215
  • privatkey.JPG
    privatkey.JPG
    46.5 KB · Views: 227
  • firebase_app.JPG
    firebase_app.JPG
    55.5 KB · Views: 254
  • Manifest.JPG
    Manifest.JPG
    71.7 KB · Views: 213
Upvote 0
Top