B4A Class Dropbox API V2. All HTTP! All using httputils2

wizard699

Active Member
Licensed User
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim dbx As DropBox
End Sub
Sub Activity_Create(FirstTime As Boolean)
    dbx.Initialize(Me,"DropBox","your key here",True)
    dbx.Upload(File.DirRootExternal,"overlay.png","/","overlay.png")
End Sub
Sub DropBox_FileUpload(meta As Map)
    Log("Dropbox_FileUpload: "&meta)
 
    Dim Path As String = meta.Get("path_lower")
    Log("Path: "&Path)
    Dim rev As String = meta.Get("rev")
    Log("Rev: "&rev)
    Dim name As String = meta.Get("name")
    Log("Name: "&name)
    Dim size As Int = meta.Get("size")
    Log("FileSize: "&size)
    Dim client_modified As String = meta.Get("client_modified")
    Dim server_modified As String = meta.Get("server_modified")
    Log("ClientModified: "&client_modified)
    Log("ServerModified: "&server_modified)
End Sub
Yes DonManfred I'm sure that in last version work perfectly.
I'll be waiting last version.
 

mr23

Active Member
Licensed User
    • initialize (CallbackModule As Object, EventName As String, AccessToken As String, debug As Boolean) As String
      Initialize the Dropbox Class.
      Parameters
      CallbackModule
      EventName
      AccessToken
      debug
      <code>Example:
      dbx.GetAccount("454365363464")</code>
...

First we need to initialize the Class
B4X:
dbx.Initialize(Me,"DropBox","secretkey",True)
Don, the 'AccessToken' is shown Initialize API, but example shows 'secretkey'. The latter didn't work, but a generated access token, for testing only, does work.

https://www.dropbox.com/developers/reference/oauth-guide requires using OAuth2.

Does this API (or will it, in the future) provide a method to go through the OAuth2 flow? Maybe I missed it.
 

DonManfred

Expert
Licensed User
Does this API (or will it, in the future) provide a method to go through the OAuth2 flow? Maybe I missed it.
There is no such endpoint in the V2 Api as yet. Maybe they will provide one in future. If they do i´ll extend my class to the new endpoints.

For now there is no such method in the Api. and due to this it is not part of my Class.

But you can surely extends the class (source is in post #1) with some API V1 calls which will do such Requests and then replace the secret key with the new auth-token
 

DonManfred

Expert
Licensed User
UPDATE: Please note that the code here is for Authenticating against the V1 api.

Do not use this example anymore.

Does this API (or will it, in the future) provide a method to go through the OAuth2 flow?
If you want to use "the users" Dropbox token (or even to get the token through the OAuth2-flow) you can use this

This is an simple Example providing a activity with a webview to do the tokenrequest.
B4X:
Sub Process_Globals
    Dim dbx As DropBox
    Dim clientID As String
    Dim accesstoken As String
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = True Then
        clientID = "8bgblt25ldqwh70" ' Put your ClientID of your app here
        accesstoken = "" ' define an empty token
    End If
    Activity.LoadLayout("LayoutMain")

    dbx.Initialize(Me,"DropBox","",True) ' Note that in initialization of DropboxClass the token is set to an empty string.


Sub SetDropboxToken(token As String)
    dbx.AccessToken = accesstoken
    dbx.ListFolders()
End Sub
Additional there is a new Activity in the example
B4X:
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("OAuth")
    '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 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,"SetDropboxToken",token)
        Activity.Finish
        Return False
    End If

End Sub
Additional info:
B4X:
dbx.Initialize(Me,"DropBox","",True) ' Note that in initialization of DropboxClass the token is set to an empty string.
Note that you are not allowed to do any DropboxMethodCall unless you go through the OAuth-Flow (in this example start the OAuth activity) as they will not work due to the missing access_token!!


Edit:
Replaced the Example with a new whichis now Authenticating against the V2 api.

 

Attachments

Last edited:

Esteve

Member
Licensed User
Hello, I am a newbie in B4A, few days ago I bought the license.
I'd like to handle errors initialize dbx.
Is there an event that occured if, for example,"access_token" fails?

Thanks!
Sorry for my English...
 

DonManfred

Expert
Licensed User
Hello, I am a newbie in B4A, few days ago I bought the license.
Good decision :)

Welcome in the community!

Is there an event that occured if, for example,"access_token" fails?
You did not make any Dropbox-Call when initializing the class. So, there could not be any error...

But you will get an error when calling one of the methods if the token is wrong i guess.
 

Esteve

Member
Licensed User
Thanks!!

OK, what happens is that when I call the method dbx.getcurrentaccount with a invalid acces token, I receive a log of the error but the event is not activated, then not be how to capture this error.:(
 

Reinierus

Member
Licensed User
Hello DonManfred.
I am trying to use the example, but there is a library that I don't have and don't see in the dependencies.
This is one of the lines with the error:

B4X:
job.GetRequest.SetContentEncoding("text/plain")
upload_2016-2-18_12-25-45.png


What am I doing wrong?

Thanks a lot
 

incendio

Well-Known Member
Licensed User
Great library !!
Thanks for your contribution.

I have a fews question :
1) when generate token in Dropbox developer, I saw App key & App secret, is there any use for this?

2) I have these codes
B4X:
    dbx.CreateFolder("/f2")
dbx.Upload(PUB_InstDir,"reports.png","/f2","rp5.png")
dbx.Upload(PUB_InstDir,"purchase.png","/f2","p5.png")
Folder created without checking if folder already exist. When folder does exist, JobDone raised an error "path/conflict/folder", but upload operation still OK.
Is it OK to ignore this message?
 

DonManfred

Expert
Licensed User
when generate token in Dropbox developer, I saw App key & App secret, is there any use for this?
You can directly use it in your app when initialization of th class.
If you use this tokens the class will directly and only work with YOUR dropbox then.
Is it OK to ignore this message?
From principle, yes.
But to be safer you should test the existence of a folder first (list files/directories) and only call this command if the foder do not exist.
 

incendio

Well-Known Member
Licensed User
Thanks for your replay.

If I want to use user dropbox token (post #45), where do i get client id?
 

DonManfred

Expert
Licensed User
If I want to use user dropbox token (post #45), where do i get client id?
You use your clientID in initialization of the class. The APP is the client. Using the users token it will work with the users Dropbox.
Using your token (not using the OAuth way) it will use your dropbox.
 

incendio

Well-Known Member
Licensed User
You use your clientID in initialization of the class. The APP is the client. Using the users token it will work with the users Dropbox.
Using your token (not using the OAuth way) it will use your dropbox.
Sorry, I don't understand.

I want to use users token/using the OAuth way, how to get clientId?
 

DonManfred

Expert
Licensed User
how to get clientId?
You dont need. The App needs the ClientID (your client-ID found in your Developerconsole at Dropbox).

The token itself is used to allow access to a specific Dropbox. In your case the Dropbox of your Customer. But you dont need a Client-ID of your Customer.
If you write the app for a customer. Then maybe he need to give you the Client-ID from his Developerconsole so you can use this Client-ID in your app then...
 

incendio

Well-Known Member
Licensed User
You dont need. The App needs the ClientID (your client-ID found in your Developerconsole at Dropbox).

The token itself is used to allow access to a specific Dropbox. In your case the Dropbox of your Customer. But you dont need a Client-ID of your Customer.
If you write the app for a customer. Then maybe he need to give you the Client-ID from his Developerconsole so you can use this Client-ID in your app then...
If not mistaken, in this case, application published in play store can't directly backup and restore to user's Dropbox since application don't know client id, is that right?

Btw, just found that client id is an app key in developers console.
 
Last edited:

DonManfred

Expert
Licensed User
can't directly backup and restore to user's Dropbox since application don't know client id
Can´t directly backup or restore cause the app has no Token at this time. The app-user need togo through the OAuth-Flow to let the app get a token to work with. Yes.
Btw, just found that client id is an app key in developers console.
That´s what i said. The APP uses the Client-ID
 

DonManfred

Expert
Licensed User
You can ask the app-user for client-ID and maybe the token. (Two editfields)
The user enters the data and your app the reinitialize with the entered client and token.
This way you dont need to let the user go over the OAuth-Flow to get an token.
You app can store the entered data in sqlite-db or so. And on App-start you read the credentials and use them in Initialize. So. doing this allows you then to directly work with the users dropbox
 

incendio

Well-Known Member
Licensed User
You can ask the app-user for client-ID and maybe the token. (Two editfields)
The user enters the data and your app the reinitialize with the entered client and token.
This way you dont need to let the user go over the OAuth-Flow to get an token.
You app can store the entered data in sqlite-db or so. And on App-start you read the credentials and use them in Initialize. So. doing this allows you then to directly work with the users dropbox
This method required users to create Dropbox's app, i am afraid not all users know how to do this.
Is there no other methods? I was thinking if it's possible, users only input their Dropbox's account & password, with this info, application can do backup & restore operations.
 
Top