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

egurik's avatar
egurik
Explorer | Level 4
4 years ago

Implementing refresh tokens for objective-c applications

Hello there.

 

So, according to Your reminder: "on September 30th, 2021, the Dropbox OAuth flow will no longer return new long-lived access tokens. It will instead return short-lived access tokens, and optionally return refresh tokens." I have been started to update my existing iOS objective-c application.

 

The first step is to change [DBClientsManager authorizeFromController...] to [DBClientsManager authorizeFromControllerV2].

 

Here is an example of this:

DBScopeRequest *scopeRequest =

        [[DBScopeRequest alloc] initWithScopeType:DBScopeTypeUser

                                           scopes:@[]

                             includeGrantedScopes:NO];

    

    [DBClientsManager authorizeFromControllerV2:[UIApplication sharedApplication]

                                     controller:self

                          loadingStatusDelegate:nil

                                        openURL:^(NSURL *url) {

        NSLog(@"[openURL: %@]", url);

        [[UIApplication sharedApplication] openExternalURL:url];

    }

                                   scopeRequest:scopeRequest];

 

Then I catch the auth-response:

 

[DBClientsManager handleRedirectURL:url

                             completion:^(DBOAuthResult * __nullable authResult) {

        

        NSLog(@"[authResult: %@]", authResult);

    }];

 

The authResult contains not nil 'refreshToken' and 'tokenExpirationTimestamp' what is a good sign. However, the 'tokenExpirationTimestamp' has a giant value (as I understand expressed via seconds). It's not clear how I should calculate the expiration date. It might be this huge value was started from some date in the past (1970)? Ok, let's suppose I should not calculate the expiration date of new short-lived access tokens. I have a single place for all my requests to/from Dropbox. I decided that in this place I will check the requestError (networkError) and perform refreshing if the Dropbox returns Auth-error. Is it correct? And the second question: how can I check this code? When refresh tokens should expire on average? I was waiting for ~1-2 hours BUT the Dropbox did not return Auth errors.

 

- (BOOL)doInternalWorkSynchronously {

    

    BOOL done = NO;

    

    do

    {

        done = [self doInternalTaskSynchronously];

        

        NSLog(@"[requestError: %@]", _requestError);

        

        if ([_requestError isRateLimitError]) {

            

            [self waitBeforeMakingAnyAdditionalRequests];

        }

        else if ([_requestError isAuthError]) {

            

            DBRequestAuthError *authError = [_requestError asAuthError];

            

            NSLog(@"[authError: %@]", authError);

            

            [self refreshAccessToken];

        }

        else {

            

            // Skip processing of other types the request error.

            break;

        }

        

    } while (!_isCancelled);

    

    return done;

}

 

- (void)refreshAccessToken {

    

    dispatch_semaphore_t refreshTokenSemaphore = dispatch_semaphore_create(0);

    

    DBOAuthManager *dbOAuthManager = [DBOAuthManager sharedOAuthManager];

    

    DBAccessToken *accessToken = [dbOAuthManager retrieveFirstAccessToken];

    

    [dbOAuthManager refreshAccessToken:accessToken

                                scopes:@[]

                                queue:nil

                            completion:^(DBOAuthResult * __nullable authResult) {

        

        dispatch_semaphore_signal(refreshTokenSemaphore);

    

        KPLog(@"[authResult: %@]", authResult);

    }];

    

    dispatch_semaphore_wait(refreshTokenSemaphore, DISPATCH_TIME_FOREVER);

}

 

 

 

 

 

 

  • The tokenExpirationTimestamp is a unix timestamp.  Parsing it should show ~4 hours until expiration, but expiration time is subject to change and should not be hardcoded. 

     

    The SDK should be handling requesting an updated access token from a refresh token for you.

     

    Refresh tokens don't expire; though end users can revoke the authentication.

  • kylea's avatar
    kylea
    Icon for Dropbox Staff rankDropbox Staff

    The tokenExpirationTimestamp is a unix timestamp.  Parsing it should show ~4 hours until expiration, but expiration time is subject to change and should not be hardcoded. 

     

    The SDK should be handling requesting an updated access token from a refresh token for you.

     

    Refresh tokens don't expire; though end users can revoke the authentication.

    • egurik's avatar
      egurik
      Explorer | Level 4

      Thanks for the reply!

       

      The tokenExpirationTimestamp is a unix timestamp.  Parsing it should show ~4 hours until expiration, but expiration time is subject to change and should not be hardcoded. 

      Thanks. Good to know. Although it wouldn't be bad if this fact was reflected in the documentation (with measure units + examples).

       

      Refresh tokens don't expire; though end users can revoke the authentication.

      Yes, sorry. I meant a short-lived access token.

       

      The SDK should be handling requesting an updated access token from a refresh token for you.

      It's of course very cool. Thanks. Just to confirm that we are on the same page: I should not call DBOAuthManager.

      refreshAccessToken explicitly? If a user does not sign out by himself then the SDK will not return the error 401?

       

       

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        That's correct, you don't need to call refreshAccessToken yourself. As long as you set things up as instructed, the SDK will handle all of that for you.

         

        And yes, as long as nothing specifically happens to invalidate the refresh token (such as the user unlinking the app, deleting their account, etc.) the refresh token will continue to be usable.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,883 PostsLatest Activity: 8 hours ago
326 Following

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!