Start 2025 on time and up to date! Seamlessly integrate your calendars into Dropbox with these simple steps! 📆
Forum Discussion
Mostafa Ezzat
3 years agoExplorer | Level 4
Issue in generating access token
Hello, I faced many issues in generating access token
First, I have here access code generated <REDACTED> Second trying to execute this curl :
curl https://api.dropbox.com/oauth2/token...
- 3 years ago
Hi Mostafa Ezzat,
Let's try some authentication process step by step. 🙂 It may succeed.
At the beginning make sure you have your App key and App secret at hand from App Console page. Select desired application there and once got there in and scroll to field "App key" and "App secret" (for the secret "Show" should be used) keep the browser window accessible, so would be able take a look there when needed.
Next, open a new browser window and put into address line following:
https://www.dropbox.com/oauth2/authorize?token_access_type=offline&response_type=code&client_id=<App key>
Where "<App key>" is the one from you previous browser window. Next the confirmation you will get a code (alphanumeric sequence). The same could be received automatic when redirect URL is in use (either direct or PKCE code flow), but here we will perform it in such a way for clarity.
Next step will be to "materialize" the received code. In a terminal window execute following curl command:
curl https://api.dropbox.com/oauth2/token -d code=<received code> -d grant_type=authorization_code -u <App key>:<App secret>
Where "<received code>" is the code shown up in the second browser window after confirmation. "<App key>" and "<App secret>" come from the first browser window. As a result you will get in your terminal something like:
{"access_token": "sl.abcdefg123456789AbCdEf-GHijKLmn0U", "token_type": "bearer", "expires_in": 14400, "refresh_token": "oDfT54975DfGh12345KlMnOpQrSt01a", "scope": "account_info.read files.content.read etc.", "uid": "123456789", "account_id": "dbid:ABCDEF5g8HijklMNopQ2Rs5tUV_wxy5z_YO4"}
Of course, you will receive different values filling the pattern. Here "sl.abcdefg123456789AbCdEf-GHijKLmn0U" is access token you can use in every regular API call for "14400" second since current moment until expires. "oDfT54975DfGh12345KlMnOpQrSt01a" is your refresh token. The one that will never expire (or till revoke).
When currently received access token expires, you can perform following curl call:
curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=oDfT54975DfGh12345KlMnOpQrSt01a -u <App key>:<App secret>
Where "oDfT54975DfGh12345KlMnOpQrSt01a" is the refresh token "materialized" from code at the beginning. "<App key>" and "<App secret>" come again from the first browser window. As a result you will get in your terminal something like:
{"access_token": "sl.abcdefg123456789AbCdEf-OPqrSTuv1W", "token_type": "bearer", "expires_in": 14400}
Again "sl.abcdefg123456789AbCdEf-OPqrSTuv1W" is an access token usable in regular API calls for "14400" seconds (i.e. 4 hours). The last call need to be used every time you need valid access token and the previous one got expired. For the test here you don't have to wait 4 hours. You can call it immediately. 😉 Completes everything successfully?
Every time you do receive access token, it can be use for as many seconds as denoted in "expires_in" field. The access token itself is a ASCII chars sequence and you should be ready to process such a sequence as presented (including different length).
Hope this gives direction and clarifies matter with the step by step processing.
Mostafa Ezzat
3 years agoExplorer | Level 4
I'm sorry for this but it doesn't work, I also tried with no quotes refresh_token=refresh_token it prints this error
"error_description": "The request parameters do not match any of the supported authorization flows. Please refer to the API documentation for the correct parameters."
curl https://api.dropbox.com/oauth2/token \ -d grant_type=refresh_token \ -d refresh_token='refresh_token' \ -u mykey:mykey
May I ask something if this the return of the refresh token but the access_token is totally different size from the one which genrated by the first curl so is that normal with this access token I'll be able to execute some curl codes? . Thanks in advance
{ "access_token": "sl.abcd1234efg", "expires_in": "13220", "token_type": "bearer", }
Здравко
3 years agoLegendary | Level 20
Hi Mostafa Ezzat,
Let's try some authentication process step by step. 🙂 It may succeed.
At the beginning make sure you have your App key and App secret at hand from App Console page. Select desired application there and once got there in and scroll to field "App key" and "App secret" (for the secret "Show" should be used) keep the browser window accessible, so would be able take a look there when needed.
Next, open a new browser window and put into address line following:
https://www.dropbox.com/oauth2/authorize?token_access_type=offline&response_type=code&client_id=<App key>
Where "<App key>" is the one from you previous browser window. Next the confirmation you will get a code (alphanumeric sequence). The same could be received automatic when redirect URL is in use (either direct or PKCE code flow), but here we will perform it in such a way for clarity.
Next step will be to "materialize" the received code. In a terminal window execute following curl command:
curl https://api.dropbox.com/oauth2/token -d code=<received code> -d grant_type=authorization_code -u <App key>:<App secret>
Where "<received code>" is the code shown up in the second browser window after confirmation. "<App key>" and "<App secret>" come from the first browser window. As a result you will get in your terminal something like:
{"access_token": "sl.abcdefg123456789AbCdEf-GHijKLmn0U", "token_type": "bearer", "expires_in": 14400, "refresh_token": "oDfT54975DfGh12345KlMnOpQrSt01a", "scope": "account_info.read files.content.read etc.", "uid": "123456789", "account_id": "dbid:ABCDEF5g8HijklMNopQ2Rs5tUV_wxy5z_YO4"}
Of course, you will receive different values filling the pattern. Here "sl.abcdefg123456789AbCdEf-GHijKLmn0U" is access token you can use in every regular API call for "14400" second since current moment until expires. "oDfT54975DfGh12345KlMnOpQrSt01a" is your refresh token. The one that will never expire (or till revoke).
When currently received access token expires, you can perform following curl call:
curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=oDfT54975DfGh12345KlMnOpQrSt01a -u <App key>:<App secret>
Where "oDfT54975DfGh12345KlMnOpQrSt01a" is the refresh token "materialized" from code at the beginning. "<App key>" and "<App secret>" come again from the first browser window. As a result you will get in your terminal something like:
{"access_token": "sl.abcdefg123456789AbCdEf-OPqrSTuv1W", "token_type": "bearer", "expires_in": 14400}
Again "sl.abcdefg123456789AbCdEf-OPqrSTuv1W" is an access token usable in regular API calls for "14400" seconds (i.e. 4 hours). The last call need to be used every time you need valid access token and the previous one got expired. For the test here you don't have to wait 4 hours. You can call it immediately. 😉 Completes everything successfully?
Every time you do receive access token, it can be use for as many seconds as denoted in "expires_in" field. The access token itself is a ASCII chars sequence and you should be ready to process such a sequence as presented (including different length).
Hope this gives direction and clarifies matter with the step by step processing.
- Greg-DB3 years agoDropbox Staff
Mostafa Ezzat I see Здравко very helpfully offered a detailed walk-through of this flow. Please let us know if this still isn't working for you.
- Mostafa Ezzat3 years agoExplorer | Level 4
Thanks for all it's working
- FARO3 years agoNew member | Level 2
Hi Здравко
It's possible obtain the code in this call :
https://www.dropbox.com/oauth2/authorize?token_access_type=offline&response_type=code&client_id=<App key>
permanently? When I send a request, the code is only available for a first time, when I used in a future call, the code is invalid.
Thanks for all
- Здравко3 years agoLegendary | Level 20
FARO wrote:... When I send a request, the code is only available for a first time, when I used in a future call, the code is invalid.
...
Hi FARO,
🙂How long you expect a code to be used?! 🧐
Let's see. If you forget your credentials to some service and there is a option to receive a recovery code throughout a registered phone number with SMS (for example), how many times you can use received code??? 🤔 Would be reliable and secure if such code can be used for multiple recovering? (such codes are usually relatively short and simple, so easy to use, but to guess too) 😯 That's why such codes are "single shot" (i.e. one time use) and usually for limited time.
The code you are talking about is a member of the same "team" of codes. It's used for one time authorization (together with all other provided information). The refresh token you are receiving on call authenticated successfully with this code can be used multiple times, not the code itself! 😉 Take care! As far as I can guess, according your description, you ignored actual refresh token all the time. Don't ignore it, but keep and use it on followup access token refresh (exactly as mentioned - follow it exactly and don't try to interpret and modify, if your aren't sure what exactly you are doing).
Hope this clarifies matter.
- Greg-DB3 years agoDropbox Staff
FARO Здравко is correct; that code is an "authorization code", which can only be used once each. You should process that and instead store and re-use the "refresh token", which can be used repeatedly. Check out the OAuth Guide and authorization documentation for more information.
- matthewknill3 years agoExplorer | Level 3
I'm getting the following error:
{"error": "invalid_request","error_description": "Bad \"Authorization\" header: 'ascii' codec can't decode byte 0xe2 in position 15: ordinal not in range(128)"} - Здравко3 years agoLegendary | Level 20
Hi matthewknill,
The message, you posted, is clear enough - your authorization header is malformed. Are you certain your header matches the pattern:
Authorization: Bearer <Your access token here>
According to the data in your post, it's not. 🤔 How exactly have you formatted it?
Hope this gives direction.
- matthewknill3 years agoExplorer | Level 3
I've followed process exactly then used the following to get the refresh token:
curl https://api.dropbox.com/oauth2/token \ -d code=<Access Code> \ -d grant_type=authorization_code \ -u <App key>:<App secret>
- Здравко3 years agoLegendary | Level 20
In such a case it sounds like some copy/paste error. Make sure that everything enlabeled like "<something>" is replaced exactly to what the something means! 🤷 Don't put anything more or less there. That's it.
- matthewknill3 years agoExplorer | Level 3
Hmm, I thought I did copy it accurately but maybe not, I did it again and it seemed to work.
Thanks and sorry for the hassle...
- Kloss3 years agoNew member | Level 2
I was able to get my access code generated.
Then i open terminal as admin - and do the curl command replacing the app key and app secrete and inserting the access code.
Red XXXXX below are my account codes but replaced with xxxxx's
However I get a red error in Terminal. it says
Invoke-WebRequest : Parameter cannot be processed because the parameter name 'u' is ambiguous. Possible matches include: -UseBasicParsing -Uri -UseDefaultCredentials -UserAgent.
At line:1 char:128
+ ... xxxxxxxxxxxxxxxxxxxx -d grant_type=authorization_code -u xxxxxxx ...
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.InvokeWebRequestCommand:EDIT:I googled and found article to do the following command inside Terminal to fix this issue. - That seemed to work so I'll paste it here for anyone else who runs into this issue on Windows 11.
Remove-item alias:curl
Now I'm stuck at the message
"Enter host password for user 'xxxxxxxxxxxxxxxxxxxxxxxxx':
I've Typed password - I've copy pasted but the error I keep getting is
{"error": "invalid_client: Invalid client_id or client_secret"}
I know my app key and app secret is correct. And the host password I assume is my dropbox password to get into my account.
any thoughts on this? - Greg-DB3 years agoDropbox Staff
Kloss The original "-u" option shown earlier in this thread is an option for curl for specifying the Basic credentials (which in this case should be the app key and secret). It looks like in your environment it was getting sent to Invoke-WebRequest, where "-u" is ambiguous. I'm glad to hear you already sorted that out.
For this /oauth2/token call, you would specify the app key and secret, but not your account password. The "-u" option is a way to specify the app key and secret, in place of what would be the username and password in other contexts. So, to specify the app key and secret, you could use it like "-u <appkey>:<appsecret>", where <appkey> is your app key, and <appsecret> is your app secret; do not enter your account password.
- Здравко3 years agoLegendary | Level 20
Kloss wrote:...
I know my app key and app secret is correct. And the host password I assume is my dropbox password to get into my account.
...Kloss, your account id (email) or password have nothing to do here! The only thing you need to authenticate is your application, nothing more. Check your shell syntax (might be some typing error, for instance). You account identification has already been performed in your browser. 😉
Good luck.
- astronomy3 years agoNew member | Level 2
Thank you. Your answer was very helpful to me. It should be in the guide articles.
- calexmac3 years agoNew member | Level 2
Hi Здравко
I have followed your (very generous) steps. Every works fine... I get back the JSON I am expecting as per your post. ie I get the access token, a refresh token and bunch of other data. However when I use the 'sl...' access token just generated to upload a file, I get the following error:
{"error_summary": "expired_access_token/...", "error": {".tag": "expired_access_token"}}
I have tried the flow you recommend several times to ensure I have all inputs correct, and get the same error.
Then I tried generating an access token directly from the app console for the app. This returned the same error.
Any thoughts on where I should focus?
tx
c
- Greg-DB3 years agoDropbox Staff
calexmac An 'expired_access_token' error indicates that the particular short-lived access token you used has expired, so you'd need to get a new one to continue making API calls. If you have a refresh token, you can do so by calling /oauth2/token with grant_type=refresh_token. Please refer to the messages at the beginning of this thread for more information.
- The Kingdom3 years agoExplorer | Level 3
Does this method still work until now?
- Здравко3 years agoLegendary | Level 20
Yes The Kingdom, it does. It has never stopped. 😉 Do you have any issue? 🧐
Nice New Year holiday for everybody!
- The Kingdom3 years agoExplorer | Level 3Can you help me for this method? I don't have coding skill and i don't understand about coding.
- Здравко3 years agoLegendary | Level 20
Hm.. 🤔 To be honest, I'm not sure what you ask me for. API/SDKs are software tools directed for usage in software project (or even simple client script). This is valid for any kind of such tools (no limit to Dropbox API/SDK). In the same context you need some basic coding skills, at least! My example here shows how authentication can work in a very basic and simplistic way. It's not the only way. The same way can be used for some prebuild third party software, but better ask the software provider how exactly, if you don'f feel yourself confident enough.
Hope this gives direction.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,936 PostsLatest Activity: 3 hours ago
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!