Android Question DropBoxV2. How know if token is expired?

Juan Vargas (Bioagro)

Member
Licensed User
I have the last token used stored. If I use that token and is not valid, I got several errors.
Is there a way to test if a token is valid, before start any operation?. If is not valid, then I launch the oAuth to get a new one.
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
        If auth.OAuth2Token <> Null Then
            If auth.OAuth2Token <> "" Then
                token = auth.OAuth2Token
                Dim cred As DbxCredential  = auth.DbxCredential
                
                Log($"cred.AccessToken: ${cred.AccessToken}"$)
                Log($"cred.aboutToExpire: ${cred.aboutToExpire}"$)
                Log($"cred.ExpiresAt: ${cred.ExpiresAt}"$)
                Log($"cred.RefreshToken: ${cred.RefreshToken}"$)
                Log("Token available. Dropbox enabled")
                Log("Token "&token)
                Starter.kvs.Put("dbxtokenv2", token)
            Else
                Log($"Token = """$)
            End If
        Else
            Log("OAuth2Token is null. This is usually OK. Do not log it. t will only become not null after you start a Authentification flow...")
        End If
 
Upvote 0

Juan Vargas (Bioagro)

Member
Licensed User
B4X:
        If auth.OAuth2Token <> Null Then
            If auth.OAuth2Token <> "" Then
                token = auth.OAuth2Token
                Dim cred As DbxCredential  = auth.DbxCredential
               
                Log($"cred.AccessToken: ${cred.AccessToken}"$)
                Log($"cred.aboutToExpire: ${cred.aboutToExpire}"$)
                Log($"cred.ExpiresAt: ${cred.ExpiresAt}"$)
                Log($"cred.RefreshToken: ${cred.RefreshToken}"$)
                Log("Token available. Dropbox enabled")
                Log("Token "&token)
                Starter.kvs.Put("dbxtokenv2", token)
            Else
                Log($"Token = """$)
            End If
        Else
            Log("OAuth2Token is null. This is usually OK. Do not log it. t will only become not null after you start a Authentification flow...")
        End If
Can I have your dbxAuth module? Even if I have a valid token, the code above always returns null.
 
Upvote 0

Juan Vargas (Bioagro)

Member
Licensed User
This is my dbxAuth module

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private www As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("dropboxauth")
    'Main.dbx.request_auth
End Sub

Sub Activity_Resume
    www.LoadUrl("https://www.dropbox.com/1/oauth2/authorize?client_id="& Main.clientID &"&response_type=token&redirect_uri=http://127.0.0.1/")

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub www_PageFinished (Url As String)
    Log($"www_PageFinished(${Url})"$)
    ' http://127.0.0.1/#access_token=ToZD0ArtpcUAAAAAAADb-4YF3VNuCQNZz_DK5B2cTcKI0qQAZBIU3z-EdizUT2bP&token_type=bearer&uid=189761154
    
End Sub
Sub Iniciar
    
End Sub
Sub www_OverrideUrl (Url As String) As Boolean
    Log($"www_OverrideUrl(${Url})"$)
    If Url.Contains("access_token=") Then
        ' Access Token available... Cut them off the url
        Dim token As String = Url.SubString2(Url.IndexOf("access_token=")+13,Url.IndexOf("&"))
        Log("Token found... Setting global var")
        Main.accesstoken = token
        Log($"Token "${token}" is set... Finishing activity now..."$)
        'Log(token)
        CallSubDelayed2(Main,"ResultadoToken",token)
        Activity.Finish
        Return False
    End If

End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim auth As DbxAuth
    Dim config As DbxRequestConfig

    config.Initialize("","clientidentifier","de-de",5) ' de-de is my locale
    auth.Initialize("yourappkey") ' in activity create

B4X:
Sub btnStartOAuth_Click
    isAuthFlowLogin = True
    auth.startOAuth2PKCE(config)
End Sub

B4X:
Sub Activity_Resume
    If isAuthFlowLogin Then
        If auth.OAuth2Token <> Null Then
            If auth.OAuth2Token <> "" Then
                token = auth.OAuth2Token
                Dim cred As DbxCredential  = auth.DbxCredential
                
                Log($"cred.AccessToken: ${cred.AccessToken}"$)
                Log($"cred.aboutToExpire: ${cred.aboutToExpire}"$)
                Log($"cred.ExpiresAt: ${cred.ExpiresAt}"$)
                Log($"cred.RefreshToken: ${cred.RefreshToken}"$)
                lblToken.Text = token
                Log("Token available. Dropbox enabled")
                Log("Token "&token)
                DropboxEnabled = True
                btnListroot.Enabled = True
                Starter.kvs.Put("dbxtokenv2", token)
                Dim dbxhost As DbxHost
                dbxhost.Initialize
                client.Initialize("Dropbox",config,token,dbxhost)
                dbxFiles = client.files
                dbxFiles.setEventname("dbxFiles")
                dbxSharing = client.sharing
                dbxSharing.setEventname("dbxSharing")
                dbxUsers = client.users
                dbxUsers.setEventname("dbxUsers")
                dbxUsers.CurrentAccount
            Else
                Log($"Token = """$)
            End If
        Else
            Log("OAuth2Token is null. This is usually OK. Do not log it. t will only become not null after you start a Authentification flow...")
        End If
    End If
End Sub
 
Upvote 0

Juan Vargas (Bioagro)

Member
Licensed User
B4X:
    Dim auth As DbxAuth
    Dim config As DbxRequestConfig

    config.Initialize("","clientidentifier","de-de",5) ' de-de is my locale
    auth.Initialize("yourappkey") ' in activity create

B4X:
Sub btnStartOAuth_Click
    isAuthFlowLogin = True
    auth.startOAuth2PKCE(config)
End Sub

B4X:
Sub Activity_Resume
    If isAuthFlowLogin Then
        If auth.OAuth2Token <> Null Then
            If auth.OAuth2Token <> "" Then
                token = auth.OAuth2Token
                Dim cred As DbxCredential  = auth.DbxCredential
               
                Log($"cred.AccessToken: ${cred.AccessToken}"$)
                Log($"cred.aboutToExpire: ${cred.aboutToExpire}"$)
                Log($"cred.ExpiresAt: ${cred.ExpiresAt}"$)
                Log($"cred.RefreshToken: ${cred.RefreshToken}"$)
                lblToken.Text = token
                Log("Token available. Dropbox enabled")
                Log("Token "&token)
                DropboxEnabled = True
                btnListroot.Enabled = True
                Starter.kvs.Put("dbxtokenv2", token)
                Dim dbxhost As DbxHost
                dbxhost.Initialize
                client.Initialize("Dropbox",config,token,dbxhost)
                dbxFiles = client.files
                dbxFiles.setEventname("dbxFiles")
                dbxSharing = client.sharing
                dbxSharing.setEventname("dbxSharing")
                dbxUsers = client.users
                dbxUsers.setEventname("dbxUsers")
                dbxUsers.CurrentAccount
            Else
                Log($"Token = """$)
            End If
        Else
            Log("OAuth2Token is null. This is usually OK. Do not log it. t will only become not null after you start a Authentification flow...")
        End If
    End If
End Sub


Im getting this from auth.startOAuth2PKCE(config)

B4X:
Token available (Activity Create. Try to enable Dropbox with this token
OAuth2Token is null. This is usually OK. Do not log it. t will only become not null after you start a Authentification flow...
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
    at com.dropbox.core.android.Auth$Companion.startOAuth2PKCE(Unknown Source:2)
    at com.dropbox.core.android.Auth.startOAuth2PKCE(Unknown Source:2)
    at de.donmanfred.dbxv2.AuthWrapper.startOAuth2PKCE(AuthWrapper.java:42)
    at com.tropic.invoice.main._btnbackupdropbox_click(main.java:32559)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7465)
    at android.view.View.performClickInternal(View.java:7442)
    at android.view.View.access$3600(View.java:813)
    at android.view.View$PerformClick.run(View.java:28394)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:250)
    at android.app.ActivityThread.main(ActivityThread.java:7803)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
    ... 24 more
** Activity (main) Pause, UserClosed = false **


I better wait until you post a complete example.......
 
Upvote 0
Top