Search code examples
dropbox-api

Do you need to use the OAuth flow to access your own dropbox account?


I'm writing a dropbox integration against my own account. When file get dropped I respond to a webhook notification and import the files into one of our backend systems.

It's all done in back end server code and there is no real opportunity to pop up a UI to get me to sign in.

I've developed it so far using the access token you can get from the app console but that expires after a few hours.

Are there any auth shortcuts when using the API with just your own account?


Solution

  • I've figured out some shortcuts for myself

    First off all as its a background process it needs to be running with "offline" access and using refresh tokens to acquire short lived access tokens.

    As there is no UI and its only for my own account I can get an authorisation code via the browser from this URL

    https://www.dropbox.com/oauth2/authorize?client_id={{Your APP ID Here}}&token_access_type=offline&response_type=code

    then use POSTMAN to get an access Token & Refresh Token:

    In Postman:
    
    Post to https://api.dropboxapi.com/oauth2/token
    Authorisation Basic
    UserName = {{AppKey}}
    Password = {{AppSecret}}
    
    
    Body :x-ww-form-urlencoded
    code = <<Auth Code From Browser>>
    grant_type = authorization_code
    
    

    This produces a result with an access_token and a refresh_token

    The refresh token will only expire if I withdraw access so that will be safe to add into my config and use to request access tokens or connect to the client.

    In my case its c#

    using (var dbx = new DropboxClient(refreshToken,appKey,appSecret))
    {
        var myAccount = await dbx.Users.GetCurrentAccountAsync();
        String.Format("{0} - {1}", 
            myAccount.Name.DisplayName, 
            myAccount.Email)
           .Dump("Account Details");
    }