You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

CTO1's avatar
CTO1
Explorer | Level 3
3 years ago

Using Refresh Token

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?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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's avatar
      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:

      can you tell me where is wrong the code please?

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox 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).

    • CTO1's avatar
      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's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox 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.