cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
If you’ve changed your email address, now's the perfect time to update it on your Dropbox account and we’re here to help! Learn more here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get an Access Token from a Refresh token from within B4A

How to get an Access Token from a Refresh token from within B4A

Brad Henderson
Explorer | Level 4
Go to solution

Hello All,

 

I have created a refresh token using the method thanks to "Здравко".  Manually using a web browser and Windows Command prompt.  The command prompt is: 

curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=<Refresh Token> -u <App key>:<App secret>

 

However, I want to be able to execute this from within my app, which is written in B4A.   My app is an in-house app for private use only.  From my limited knowledge of http, there is an option with "httpjob.PostString"  (B4A)  that requires parameters  link as String and text as String.  Assuming this method will work can anyone advise what the link and text parameters would need to be?

 

Your assistance is greatly appreciated.

 

Brad

1 Accepted Solution

Accepted Solutions

Здравко
Legendary | Level 20
Go to solution

Hi @Brad Henderson,

To be honest I'm not familiar with the environment you're using. On the first look though, it uses OkHttp as underlying library. So you can take a look here for a simplistic example - example how OkHttp works. It may be easier for you to encode everything in the body (i.e. pass in the body text: "grant_type=refresh_token&refresh_token=<REFRESH_TOKEN>&client_id=<APP_KEY>&client_secret=<APP_SECRET>"; replace all "<...>" tags appropriately). Find what matches to desired OkHttp calls exactly in your environment (something, I believe you may do). 😉

Hope this helps.

 

PS: Just to avoid confusion: The example supposes JSON content, but in your case the content type is different, so instead of "application/json" (as is in the example) you have to set "application/x-www-form-urlencoded" as content type (MediaType in the example).

View solution in original post

12 Replies 12

Здравко
Legendary | Level 20
Go to solution

Hi @Brad Henderson,

To be honest I'm not familiar with the environment you're using. On the first look though, it uses OkHttp as underlying library. So you can take a look here for a simplistic example - example how OkHttp works. It may be easier for you to encode everything in the body (i.e. pass in the body text: "grant_type=refresh_token&refresh_token=<REFRESH_TOKEN>&client_id=<APP_KEY>&client_secret=<APP_SECRET>"; replace all "<...>" tags appropriately). Find what matches to desired OkHttp calls exactly in your environment (something, I believe you may do). 😉

Hope this helps.

 

PS: Just to avoid confusion: The example supposes JSON content, but in your case the content type is different, so instead of "application/json" (as is in the example) you have to set "application/x-www-form-urlencoded" as content type (MediaType in the example).

Greg-DB
Dropbox Staff
Go to solution

@Brad Henderson In addition to what Здравко said, you may want to look at the documentation for the PostString method itself for information on how to use that. On the Dropbox side of things though, you can find the documentation for the Dropbox authorization endpoints here.

Brad Henderson
Explorer | Level 4
Go to solution

Thank you ,  

I will look into this.  This is new so will need to research more.

 

Thanks again

Brad Henderson
Explorer | Level 4
Go to solution

Thanks Greg,

 

I will look further at this link.

Brad Henderson
Explorer | Level 4
Go to solution

Greg,

I had a look at the documentation and it appears the command I need is this one:

curl https://api.dropbox.com/oauth2/token \
    -d grant_type=refresh_token \
    -d refresh_token=<REFRESH_TOKEN> \
    -d client_id=<APP_KEY> \
    -d client_secret=<APP_SECRET>

 

I have tried to apply this in my B4A code as follows:

Dim data As Map
	data.Initialize
	data.Put("grant_type", "refresh_token")
	data.Put("refresh_token", "REFRESH_TOKEN")
	data.Put("client_id", "APP_KEY")
	data.Put("client_secret", "APP_SECRET")
	
	Dim j As HttpJob
	j.Initialize("", Me)
	j.PostString("https://api.dropbox.com/oauth2/token",data.As(JSON).ToString)
	j.GetRequest.SetContentType("application/json")
	Wait For (j) JobDone(j As HttpJob)
	If j.Success Then
		Log(j.GetString)
	Else
		Log(j.ErrorMessage)
	End If
	
	j.Release

 

 

 

But I get this error:

ResponseError. Reason: , Response: {"error": "invalid_request", "error_description": "The request parameters do not match any of the supported authorization flows. Please refer to the API documentation for the correct parameters."}

 

While I realize you may not be familiar with this language, is there anything glaringly wrong from what you see?

 

Thanks Brad

Здравко
Legendary | Level 20
Go to solution

Hi @Brad Henderson,

I see, you have selected that url command as base for what you're going to do. I believe, you have the command tested and it works fine. Right?

Something I cannot understand though, where did you decide from that JSON is sended? 🤔 There is a way to check this. Add one more option to the curl command - '-v' option (from verbose). You will see a lot of things in your terminal - all that curl sends as request and receives back as response. This includes header blocks of both request and response. In both header blocks are corresponding Content-Type headers. Check what curl sends as request content type and tell us is it json or something else.

 

PS: Bear in mind that when you scroll your terminal back, the response (the later one) will be visible before the request (something that's first in time).

Greg-DB
Dropbox Staff
Go to solution

@Brad Henderson Здравко is correct; these parameters should be sent as application/x-www-form-urlencoded, not JSON.

Brad Henderson
Explorer | Level 4
Go to solution

Thanks Здравко,

 

Changed from JSON to "application/x-www-form-urlencoded" then created a string for the other variables in a string using a string builder.

Works perfectly.

Thanks again

Brad

 

 

 

 

Brad Henderson
Explorer | Level 4
Go to solution

Thanks Greg,

 

Yes, made this change and it worked. 

 
Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Brad Henderson Explorer | Level 4
  • User avatar
    Здравко Legendary | Level 20
What do Dropbox user levels mean?