We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.
Forum Discussion
AG15
3 years agoExplorer | Level 4
Refresh Tokens
Hello, I've been reading dozens of pages of documentation, blogs, dropbox forum posts and other information without avail. Most of the API is pretty straighforward and easy to use but I'm hitting a roadblock.
What I'm trying to do is get a refresh token. This will be a server app using a single user account. I understand the refresh token will get me new access tokens each time. My question is how to get the first refresh token? Can I do it through C# code? If not, which endpoint would I use to get the initial token? I pulled the examples from github but it doesn't compile and I failed to rebuild it (the example was built on .net 4.5 and the API is .net standard so not compatible). If you're so kind to provide a solution, can you explain it step by step or sample code (I've read the OAuth guides multiple times and they're unclear to me)?
Thanks,
Al
- Greg-DBDropbox Staff
To get a refresh token, you'll need to use the OAuth app authorization flow. It is possible to process this in C#. For the official Dropbox API v2 .NET SDK in C#, you can find an example of getting and using a refresh token in the OauthBasic example (non-PKCE, meant for server-side apps) as well as in the OAuthPKCE example (PKCE, meant for client-side apps).
If this is just for your own use though, that is, you only need to connect your own account, you don't really need to implement that in C#. You can process this manually once and copy/paste the necessary values, which would likely be easier.
For instance, you could:
- Make your OAuth app authorization URL like this: https://www.dropbox.com/oauth2/authorize?client_id=APPKEYHERE&response_type=code&token_access_type=offline (plug in your app key in place of "APPKEYHERE").
- Browse to that page in your browser while signed in to your account and click "Allow" to authorize it.
- Copy the resulting authorization code.
- Exchange the authorization code for an access token and refresh token like this, e.g., using curl on the command line: (plug in the authorization code from step 3 in place of "AUTHORIZATIONCODEHERE," the app key in place of "APPKEYHERE", and the the app secret in place of "APPSECRETHERE").
curl https://api.dropbox.com/oauth2/token \ -d code=AUTHORIZATIONCODEHERE \ -d grant_type=authorization_code \ -u APPKEYHERE:APPSECRETHERE
The response will contain a short-lived access token and refresh token that you can then plug in to your code as needed.
- AG15Explorer | Level 4
Thanks the reply Greg.
These are the specific steps I took to get my refresh token (written in a guide format)
- Register your app and get your <App Key> and <App Secret> from the App dashboard
- Add a localhost <Redirect URI> in the App dashboard’s Redirect URL section
- Insert your app key and the redirect URL you set in #2 in the following URL: https://www.dropbox.com/oauth2/authorize?client_id=<APP KEY>&redirect_uri=<YOUR_REDIRECT_URI>&response_type=code&token_access_type=offline
- For me, I just ran a local host server in Visual Studio with an endpoint for the redirect URL so I could catch it to examine the redirect URL.
- Copy and paste the URL from #3 into your browser, follow the steps on dropbox’s page and it will hit your redirect URL with an <AUTHORIZATION CODE> attached to the redirect URL
- Download CURL for windows (if you’re a Windows user)
- Open a command window in the CURL directory and type in the following: curl https://www.dropbox.com /oauth2/token –d code=<AUTHORIZATION CODE> -d grant_type=authorization_code -d redirect_uri=<REDIRECT_URI> -u <APP KEY>:<APP SECRET>
- This will return a json payload, get the refresh token from it and use it with your App Key and App Secret as parameters for the DropboxClient constructor. The Dropbox API will handle getting short-lived tokens each time it’s used. Your refresh token won’t expire.
About Discuss Dropbox Developer & API
795 PostsLatest Activity: 3 days 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!