cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Musicians, convert your MuseScore files to PDF to play music on the go! Learn more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

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

Using Refresh Token

Using Refresh Token

CTO1
Explorer | Level 3

Currently I am trying to migrate our application to the new refresh token system

I am getting my Refresh token and my ExpiresAt values, and saving that data on the database,

 

if the ExpiresAt value has already pass I am creating my object this way:

 

 dbx = new DropboxClient(settings.RefreshToken, appKey, appSecret);

 

and using that new object for my resquest, but in my view I am still getting the message:

 

Failed to access a remote cloud service
StatusCode: Unauthorized
Request result: {"error_summary": "expired_access_token/.", "error": {".tag": "expired_access_token"}}

 

where in c# I have to sent my refresh token value so it can keep the token alive when I need it?

7 Replies 7

Greg-DB
Dropbox Staff

Building a DropboxClient with the refresh token, app key, and app secret as you've shown here is correct and valid. Are you sure that is the specific DropboxClient object you're using when you're getting that 'expired_access_token' error? I just tried it and it is working for me with my own values.

 

Note that an 'ExpiresAt' value would be referring to a specific access token (not a refresh token). When building a client with the refresh token and app key/secret like that, the client won't even have an access token anyway, so it will automatically get a new valid one. That being the case, you don't actually need to store and supply the access token and expiration information to begin with, since you can always just supply the refresh token.

CTO1
Explorer | Level 3

here is where I am getting my model, the dbxService is where I send the refreshToken value as show in the previus message

public async Task<FileManagerViewModel> GetDropboxViewModel(Panther.Views.Attachment.IndexViewModel gridViewModel, IIdentityService identityService)
        {
            var dbxViewModel = new FileManagerViewModel();
            dbxViewModel.Type = FileManagerProviderType.Dropbox;
            Project project = null;
            var dropBoxsettings = await _unitOfWork.DropBoxSettingsRepository.GetSettingsByTenantId(identityService.Tenant.Id).FirstAsync().ConfigureAwait(false);
            var path = "/MyCompany";
            MyCompanyLogger.Log(MyCompanyLogLevel.Info, "BlobRepository: GetDropboxViewModel", $"GridViewModel: {Newtonsoft.Json.JsonConvert.SerializeObject(gridViewModel)}");

            try
            {
                var dbxService = new Panther.Services.DropboxService(dropBoxsettings);
                FolderMetadata folder = null;
                if (gridViewModel.AccountGuid.HasValue)
                {
                    var account = await _unitOfWork.AccountRepository.GetAccount(identityService, gridViewModel.AccountGuid.Value, AccessLevelAction.Details).FirstAsync().ConfigureAwait(false);
                    folder = await dbxService.GetFolder(account.DropboxFolderId, account.NameAndNumber);
                    if (account.DropboxFolderId != folder.Id)
                    {
                        account.DropboxFolderId = folder.Id;
                        await _unitOfWork.AccountRepository.InsertOrUpdateAndSaveAsync(account).ConfigureAwait(false);
                        MyCompanyLogger.Log(MyCompanyLogLevel.Info, "BlobRepository: GetDropboxViewModel", $"Account updated Dropbox folder Id: {account.DropboxFolderId}");
                    }
                    path = folder.PathLower;
                }
                if (gridViewModel.ProjectGuid.HasValue)
                {
                    project = await _unitOfWork.ProjectRepository.GetProject(identityService, gridViewModel.ProjectGuid.Value, AccessLevelAction.Details).FirstAsync().ConfigureAwait(false);
                    folder = await dbxService.GetFolder(project.DropboxFolderId, project.NameAndNumber, project.Account.DropboxFolderId, project.Account.NameAndNumber);
                    if (project.DropboxFolderId != folder.Id)
                    {
                        project.DropboxFolderId = folder.Id;
                        await _unitOfWork.ProjectRepository.InsertOrUpdateAndSaveAsync(project).ConfigureAwait(false);
                        MyCompanyLogger.Log(MyCompanyLogLevel.Info, "BlobRepository: GetDropboxViewModel", $"Project updated Dropbox folder Id: {project.DropboxFolderId}");
                    }
                    path = folder.PathLower;
                }

                var provider = new DropboxFileSystemProvider(path);
                DevExpress.Web.AccountManager.RegisterDropbox("Dropbox1", "AccessTokenValue1");
                provider.AccessTokenValue = Backoffice.Encryptor.Decrypt(dropBoxsettings.AuthToken);
                provider.AccountName = Backoffice.Encryptor.Decrypt(dropBoxsettings.AuthToken);
                dbxViewModel.FileSystemProvider = provider;

                dbxViewModel.IsAllowCreate = HasAccess(FilePermissions.Create, identityService, project);
                dbxViewModel.IsAllowDelete = HasAccess(FilePermissions.Delete, identityService); 
                dbxViewModel.IsAllowEdit = HasAccess(FilePermissions.Delete, identityService);
                dbxViewModel.IsAllowDownload = HasAccess(FilePermissions.Download, identityService);
            }
            catch (Exception ex)
            {
                Backoffice.ErrorHelper.LogErrorManually(ex);
                MyCompanyLogger.Log(MyCompanyLogLevel.Error, "BlobRepository: GetDropboxViewModel exception", ex);
            }
            return dbxViewModel;
        }

 but here

provider.AccessTokenValue = Backoffice.Encryptor.Decrypt(dropBoxsettings.AuthToken);

 

I ma getting a message that says that AccessTokenValue is obsolete, I am unable to find any example of how to use:

CTO1_0-1661961944175.png

can you tell me where is wrong the code please?

Greg-DB
Dropbox Staff

In this code I see references to "Panther.Services.DropboxService" and "DropboxFileSystemProvider", which are not classes/types written by Dropbox, so unfortunately I cannot offer support for those. You'll need to refer to the documentation/support resources for those third party objects.

 

Also, I don't see it referencing a refresh token anyway, just an access token. I don't know what's in your "dropBoxsettings", but on "provider" itself at least it looks like you're only setting an access token, not a refresh token.

 

New Dropbox access tokens are short-lived and are only valid for several hours, so if you're only loading a previously retrieved access token you will get the 'expired_access_token' starting a few hours after it was retrieved. The refresh tokens don't expire by default and can and should be re-used. The official Dropbox .NET SDK can accept just the refresh token, app key, and app secret so you don't even need to store an access token to use that, but I can't offer guidance on how to use those third party objects in a similar manner (or if they are even written to support refresh tokens at all).

Здравко
Legendary | Level 20

@CTO1 wrote:

...

provider.AccessTokenValue = Backoffice.Encryptor.Decrypt(dropBoxsettings.AuthToken);

I ma getting a message that says that AccessTokenValue is obsolete, I am unable to find any example of how to use:

...

 

 

 

Hi @CTO1,

First of all, why do you need access token at all??? Clarify to yourself it! 🤷 You already have a client. Why don't you use it? 😉

Next, you are using the former (got at authorization time) access token!!! - not the actualized one. 😁 Be more careful! After some time (at most 4 hours) every access token expires. That's why you are getting the posted error message. 😉

Hope this clarifies matter.

CTO1
Explorer | Level 3

 


@Greg-DB wrote:

Building a DropboxClient with the refresh token, app key, and app secret as you've shown here is correct and valid. Are you sure that is the specific DropboxClient object you're using when you're getting that 'expired_access_token' error? I just tried it and it is working for me with my own values.

 

Note that an 'ExpiresAt' value would be referring to a specific access token (not a refresh token). When building a client with the refresh token and app key/secret like that, the client won't even have an access token anyway, so it will automatically get a new valid one. That being the case, you don't actually need to store and supply the access token and expiration information to begin with, since you can always just supply the refresh token.


Thanks Greg for replaying my question

we are using a third party tool in our UI to navigate our dropbox folders and we send our AuthToken.

we don't need it for the dropbox client but is there a way to get the new AuthToken after we use RefreshToken?

Thanks in advance

Greg-DB
Dropbox Staff

No, the official Dropbox .NET SDK does not offer a way to retrieve the latest short-lived access token that it has, but I'll pass this along as a feature request. I can't promise if or when that might be implemented though.

Здравко
Legendary | Level 20

Easy workaround can be a parallel instantiation of DropboxRequestHandler class. Big part of the stuff inside, not closely related to refreshing, can be wipe out. Classes, fields and methods visibility should be changed, so be accessible outside. Using RefreshAccessToken method (already public) new/actual refresh token will be received. The received token can be accessed through OAuth2AccessToken within the options field. That's it - just a copy of existing file (and filtered) from SDK to the application. 😉

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    CTO1 Explorer | Level 3
What do Dropbox user levels mean?