B4J Question [SOLVED] Error with Supabase.

javiers

Active Member
Licensed User
Longtime User
Good afternoon, in my attempt to read the records of a table created in Supabase, I get the following error.

ResponseError. Reason: , Response: {"error":"invalid_request","error_description":"refresh_token required"}
Token received. Expires: 01/01/1970 01:00:00
Error occurred on line: 186 (Supabase_DatabaseSelect)
java.lang.ClassCastException: class b4j.example.supabase$_supabaseerror cannot be cast to class b4j.example.supabase$_supabasedatabaseresult (b4j.example.supabase$_supabaseerror and b4j.example.supabase$_supabasedatabaseresult are in unnamed module of loader 'app')
at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:124)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1119)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)

This is the code...

B4XMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private xSupabase As Supabase
    Private KEY As String
End Sub

Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    KEY = "XXXXXXXXXXXXXXX.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFqdnRiYXN3eXdvenRqbWFqZWNiIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTY5NDY3MjY5MSwiZXhwIjoyMDEwMjQ4NjkxfQ.1D9pOcghLoGnLQl0KhjWP3jbUM_lYxF0DcLGwNpvRN0"
    xSupabase.Initialize("https://XXXXXXXXXXX.supabase.co", KEY)' I rechecked anon key again.It is correct'
        xSupabase.InitializeEvents(Me,"Supabase")
    If xSupabase.IsInitialized = True Then
        xSupabase.Auth.RefreshToken
    End If
End Sub


'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click

    Dim Query As Supabase_DatabaseSelect = xSupabase.Database.SelectData
    Query.Columns("*").From("hidrantes")
    Wait For (Query.Execute) Complete (DatabaseResult As SupabaseDatabaseResult)
    xSupabase.Database.PrintTable(DatabaseResult)
End Sub
 
Solution
If xSupabase.IsInitialized = True Then xSupabase.Auth.RefreshToken End If
you dont need this.
That's the right way:
B4X:
    'We check the user is logged in, after you start the application
    'If not then we need to login the user again
    'If the user is logged in and just the refresh token is expired, then the lib. does the job for you
    Wait For (xSupabase.Auth.isUserLoggedIn) Complete (isLoggedIn As Boolean)
    
    If isLoggedIn = False Then
    
        Wait For (xSupabase.Auth.LogIn_EmailPassword("[email protected]","Test123!!")) Complete (User As SupabaseUser)
        If User.Error.Success Then
            Log("successfully logged in with " & User.Email)
        Else
            Log("Error: " &...

Mariano Ismael Castro

Active Member
Licensed User
Good afternoon, in my attempt to read the records of a table created in Supabase, I get the following error.



This is the code...

B4XMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private xSupabase As Supabase
    Private KEY As String
End Sub

Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    KEY = "XXXXXXXXXXXXXXX.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFqdnRiYXN3eXdvenRqbWFqZWNiIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTY5NDY3MjY5MSwiZXhwIjoyMDEwMjQ4NjkxfQ.1D9pOcghLoGnLQl0KhjWP3jbUM_lYxF0DcLGwNpvRN0"
    xSupabase.Initialize("https://XXXXXXXXXXX.supabase.co", KEY)' I rechecked anon key again.It is correct'
        xSupabase.InitializeEvents(Me,"Supabase")
    If xSupabase.IsInitialized = True Then
        xSupabase.Auth.RefreshToken
    End If
End Sub


'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click

    Dim Query As Supabase_DatabaseSelect = xSupabase.Database.SelectData
    Query.Columns("*").From("hidrantes")
    Wait For (Query.Execute) Complete (DatabaseResult As SupabaseDatabaseResult)
    xSupabase.Database.PrintTable(DatabaseResult)
End Sub
Hello, try deactivating the RLS... It is located within Authentication / Configuration / Policies
 
Upvote 0

javiers

Active Member
Licensed User
Longtime User
Thanks for the reply. RLS is now disabled, but gives a new error.

1695964697163.png


ResponseError. Reason: , Response: {"error":"invalid_request","error_description":"refresh_token required"}
Token received. Expires: 01/01/1970 01:00:00
ResponseError. Reason: , Response: {"error":"invalid_request","error_description":"refresh_token required"}
Token received. Expires: 01/01/1970 01:00:00
Error occurred on line: 183 (Supabase_DatabaseSelect)
java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "this._accesstoken" is null
at b4j.example.supabase_databaseselect$ResumableSub_Execute.resume(supabase_databaseselect.java:164)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1119)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
 
Upvote 0

javiers

Active Member
Licensed User
Longtime User
I delete the supabaseauthtoken.dat file, remove RefreshToken and it gives an error again.

WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
*** mainpage: B4XPage_Created
*** mainpage: B4XPage_Appear
*** mainpage: B4XPage_Resize [mainpage]
User is logged out, this user must log in again
Error occurred on line: 186 (Supabase_DatabaseSelect)
java.lang.ClassCastException: class b4j.example.supabase$_supabaseerror cannot be cast to class b4j.example.supabase$_supabasedatabaseresult (b4j.example.supabase$_supabaseerror and b4j.example.supabase$_supabasedatabaseresult are in unnamed module of loader 'app')
at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:118)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
 
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
I delete the supabaseauthtoken.dat file, remove RefreshToken and it gives an error again.
Read rows Supabase:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private xSupabase As Supabase
    Private AnonKey As String="XXXXXXX.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlqYXlqYnVod2xpcmtod2F5anpwIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTQ5MDM3MTUsImV4cCI6MjAxMDQ3OTcxNX0.l8fSH_A1DWp6a5xhI8r5227Xxnm9nJAoPzCk0TCltp4"
    Private URL As String="https://XXXX.supabase.co"
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xSupabase.Initialize(URL, AnonKey)
    xSupabase.InitializeEvents(Me,"Supabase")
End Sub

Private Sub Button1_Click
    'RLS disabled para poder leerlo
    Dim Query As Supabase_DatabaseSelect = xSupabase.Database.SelectData
    Query.Columns("*").From("Usuarios")
    Wait For (Query.Execute) Complete (DatabaseResult As SupabaseDatabaseResult)
    xSupabase.Database.PrintTable(DatabaseResult)
End Sub

This should work for you without any problems.
 
Upvote 0

javiers

Active Member
Licensed User
Longtime User
Hello, first of all, thank you for your answers, but unfortunately it keeps giving the same error.


Based on Converting API Call from Curl/Php to B4X... I get the results, but doing it with the Supabase library would be easier for me...

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private xSupabase As Supabase
    Private AnonKey  As String = "XXXXXXXX.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFqdnRiYXN3eXdvenRqbWFqZWNiIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTQ2NzI2OTEsImV4cCI6MjAxMDI0ODY5MX0.j5xqFAqkodRQdip-pmxdBhXrWpXLOFFuIRSKYPV61EU"
    Private URL As String ="https://XXXXX.supabase.co"
End Sub

Public Sub Initialize As Object
'    B4XPages.GetManager.LogEvents = True
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xSupabase.Initialize(URL, AnonKey)
    xSupabase.InitializeEvents(Me,"Supabase")
    If xSupabase.Database.IsInitialized = True Then
'        xSupabase.Auth.RefreshToken
    End If
End Sub


'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click

    'RLS disabled para poder leerlo
'    Dim Query As Supabase_DatabaseSelect = xSupabase.Database.SelectData
'    Query.Columns("*").From("hidrantes")
'    Wait For (Query.Execute) Complete (DatabaseResult As SupabaseDatabaseResult)
'    xSupabase.Database.PrintTable(DatabaseResult)
    
    Dim basicAuth As String
    basicAuth = AnonKey
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://XXXXX.supabase.co/rest/v1/hidrantes?select=*")
    j.GetRequest.SetHeader("apikey", AnonKey)
    j.GetRequest.SetHeader("Authorization", "Bearer" & basicAuth)
    j.Username="anon"
    
    Wait For (j) JobDone(j As HttpJob)
    Dim isArray As Boolean
    If j.Success = True Then
        If j.GetString.Contains("[") = True Then
            isArray = True
        End If
'        Log(j.GetString)
        Dim parser As JSONParser
        parser.Initialize(j.GetString)

'        Dim test As Object= parser.NextObject
        If isArray = True Then
            Dim lst As List
            lst = parser.NextArray
            Log("List:"&lst)
            
        Else
            Dim str As String
            str = parser.NextValue
        End If
    Else
        Log(j.ErrorMessage)
    End If
    j.Release
End Sub
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
If xSupabase.IsInitialized = True Then xSupabase.Auth.RefreshToken End If
you dont need this.
That's the right way:
B4X:
    'We check the user is logged in, after you start the application
    'If not then we need to login the user again
    'If the user is logged in and just the refresh token is expired, then the lib. does the job for you
    Wait For (xSupabase.Auth.isUserLoggedIn) Complete (isLoggedIn As Boolean)
    
    If isLoggedIn = False Then
    
        Wait For (xSupabase.Auth.LogIn_EmailPassword("[email protected]","Test123!!")) Complete (User As SupabaseUser)
        If User.Error.Success Then
            Log("successfully logged in with " & User.Email)
        Else
            Log("Error: " & User.Error.ErrorMessage)
        End If
    
    End If
 
Upvote 0
Solution

javiers

Active Member
Licensed User
Longtime User
you dont need this.
That's the right way:
B4X:
    'We check the user is logged in, after you start the application
    'If not then we need to login the user again
    'If the user is logged in and just the refresh token is expired, then the lib. does the job for you
    Wait For (xSupabase.Auth.isUserLoggedIn) Complete (isLoggedIn As Boolean)
   
    If isLoggedIn = False Then
   
        Wait For (xSupabase.Auth.LogIn_EmailPassword("[email protected]","Test123!!")) Complete (User As SupabaseUser)
        If User.Error.Success Then
            Log("successfully logged in with " & User.Email)
        Else
            Log("Error: " & User.Error.ErrorMessage)
        End If
   
    End If
Thanks for the reply!.
I had not followed the steps indicated in B4X] Supabase - Authentification SignIn with Google
 
Upvote 0

javiers

Active Member
Licensed User
Longtime User
When you log in with Google, a browser window opens to confirm the user. This is not practical for me.
When logging in with email and password, would the application open directly?

Would it be abusing if I asked for an example of how to do it?

If I try, it gives me an error. ( Being [email protected]","XXXXXXXX" the username and password I use to sing in on Supabase)

B4X:
#IF B4J
Private Sub xlbl_SignInWithGoogle_MouseClicked (EventData As MouseEvent)
#Else
Private Sub xlbl_SignInWithGoogle_Click
#End If
    
    #If B4A
    Wait For (xSupabase.Auth.SignInWithOAuth("xxx.apps.googleusercontent.com","google","profile email https://www.googleapis.com/auth/userinfo.email")) Complete (User As SupabaseUser)
    #Else If B4I
    Wait For (xSupabase.Auth.SignInWithOAuth("xxx.apps.googleusercontent.com","google","profile email https://www.googleapis.com/auth/userinfo.email")) Complete (User As SupabaseUser)
    #Else If B4J
    Wait For (xSupabase.Auth.LogIn_EmailPassword("[email protected]","XXXXXXXX")) Complete (User As SupabaseUser)

    #End If

    If User.Error.Success Then
        Log("successfully logged in with " & User.Email)
        xui.MsgboxAsync("successfully logged in with " & User.Email, "SignIn with Google")
    Else
        Log("Error: " & User.Error.ErrorMessage)
        xui.MsgboxAsync("Error: " & User.Error.ErrorMessage, "SignIn with Google")
    End If
    
End Sub

Error:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
ResponseError. Reason: , Response: {"error":"invalid_grant","error_description":"Invalid login credentials"}
Error occurred on line: 309 (Supabase_Authentication)
java.lang.NumberFormatException: For input string: "null"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:735)
    at anywheresoftware.b4a.BA.ObjectToNumber(BA.java:434)
    at b4j.example.supabase_authentication$ResumableSub_Login_EmailPassword.resume(supabase_authentication.java:187)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
When you log in with Google, a browser window opens to confirm the user. This is not practical for me.
Thats how it works.
1697045166631.png

When logging in with email and password, would the application open directly?
exact.
Would it be abusing if I asked for an example of how to do it?
You find all what you need in the auth example:

Make sure you have enabled email login in the supabase dashboard.
 
Upvote 0

javiers

Active Member
Licensed User
Longtime User
Thank you very much for the response. I'm a clumsy. I had not reviewed the post you indicated. Thanks again!!
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I had not reviewed the post you indicated.
All examples i made are in the main thread under "Examples and Tutorials"

Have a nice day.
 
Upvote 0
Top