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

Dame1701's avatar
Dame1701
New member | Level 2
3 years ago

Dropbox .net SDK, automatic refresh of access token not working

Hi there,

 

I'm using the latest (6.22.0) version of the .net API. However it seems that the access token is not being automatically refreshed causing a bad request error to be thrown when I try to access any part of the SDK, for example Users.GetCurrentAccountAsync(). I checked the supplied OAuth2 data, I am storing the AccessToken, RefreshToken and the AccessTokenExpiry time. Examining the stored values, I can see that the AccessTokenExpiry time has passed and therefore the access token is no longer valid.

 

From what I understand, the .net SDK is supposed to get a new access token automatically if it discovers that the supplied token has expired. However this does not appear to be happening. If I update the AccessToken and RefreshToken by relinking to my Dropbox account, everything starts working again. I am creating the SDK client in the following way:

 

var config = new DropboxClientConfig("ContentAgentDropboxPlugin")
{
HttpClient = httpClient
};


m_dropboxAPIClient = new Dropbox.Api.DropboxClient(Settings.AccessToken, Settings.RefreshToken, (DateTime)Settings.AccessTokenExpiry, AppKey, config);

 

The values for AccessToken, RefreshToken and AccessTokenExpiry are all retrieved using the correct OAuth2 authentication flow (which redirects to a website hosted by our company which supplies the data passed to it by the Dropbox authentication system to our app).

 

Does anyone have any idea what might be going wrong?

 

Many thanks,

 

Damien

  • Dame1701's avatar
    Dame1701
    New member | Level 2

    I've noticed that calling the method RefreshAccessToken(ScopeList) on the Dropbox client also results in a BadRequest error. Currently there seems to be no way to resolve this without sending the user right back through the whole OAuth2 authentication process.

     

    There are also no error details provided with the BadRequest error, but reauthenticating causes everything to start working again.

    • Dame1701's avatar
      Dame1701
      New member | Level 2

      I've managed to debug into the .net source code and the error returned by the DropBox API is "No auth function available for given request". It seems that the SDK does not have the facility to pass on the data retrieved from the API, once it fails to pass EnsureSuccessStatusCode() it will simply return the status code with no additional data. 

      • Dame1701's avatar
        Dame1701
        New member | Level 2
        public async Task<string> GetAccessToken(string LicenseServerAddress = null)
                {
                    try
                    {
                        // Short lived ID that allows us to identify ourselves to the casoftkeywebsite
                        string SessionID = Guid.NewGuid().ToString("N");
        
                        var redirect = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Code, AppKey, RedirectUri, SessionID, false, false, null, false, TokenAccessType.Offline, ScopeList, IncludeGrantedScopes.None, null);
        
                        // Take the user through authentiation
                        Process.Start(redirect.ToString());
        
                        string ServerAddress = null;
        
                        // Get the code from casoftkey, we store them there after authentication
                        if (!string.IsNullOrEmpty(LicenseServerAddress))
                        {
                            ServerAddress = LicenseServerAddress;
                        }
                        else
                        {
                            ServerAddress = m_engine.BorregoEngine.GetCCConfigFileValue("LicenseServerAddress");
                        }
                        
                        Root6LicensingRestClient Client = new Root6LicensingRestClient(ServerAddress);
        
                        var TokenResult = await Client.GetToken("ContentAgent", "!~P8+AF<,v$/z~Mp");
        
                        if (!string.IsNullOrEmpty(TokenResult.error))
                        {
                            if (!string.IsNullOrEmpty(TokenResult.error_description))
                            {
                                return TokenResult.error + " : " + TokenResult.error_description;
                            }
        
                            return TokenResult.error;
                        }
        
                        Client.SetAuthenticationToken(TokenResult.access_token);
                        ServerResult<DropboxOAuth2Info> result = null;
        
                        int Timeout = 60; // 60 seconds to get the code response
                        int TimeoutCounter = 0;
        
                        do
                        {
                            TimeoutCounter++;
                            result = await Client.GetDropboxCode(SessionID);
                            System.Threading.Thread.Sleep(1000);
                        }
                        while ((result == null || result.Data == null) && TimeoutCounter < Timeout && result.OperationResult != enDatabaseOperationResult.Error);
        
                        if (result !=null)
                        {
                            if (result.OperationResult == enDatabaseOperationResult.Error)
                            {
                                return "Error retrieving Dropbox code: " + result.error;
                            }
                        }
                        else if (TimeoutCounter >= Timeout && (result == null || result.Data == null))
                        {
                            return "Timed out waiting for the user to authenticate with Dropbox";
                        }
        
                        OAuth2Response CodeResponse = null;
        
                        if (result.Data.SessionID == SessionID)
                        {
                            CodeResponse = await DropboxOAuth2Helper.ProcessCodeFlowAsync(result.Data.Code, AppKey, AppSecret, RedirectUri);
        
        
                            Settings.AccessToken = CodeResponse.AccessToken;
                            Settings.RefreshToken = CodeResponse.RefreshToken;
        
                            if (CodeResponse.ExpiresAt != null)
                            {
                                Settings.AccessTokenExpiry = (DateTime)CodeResponse.ExpiresAt;
                            }
                            else Settings.AccessTokenExpiry = DateTime.MinValue;
        
                            Settings.UID = CodeResponse.Uid;
                        }
                        else
                        {
                            return "SessionID did not match the ID we submitted";
                        }
                    }
                    catch (Exception e)
                    {
                        return e.Message;
                    }
        
                    return null;
                }

        This is the code I am using to authenticate with Dropbox, maybe I am doing something wrong here?