cancel
Showing results for 
Search instead for 
Did you mean: 

How to get OAuth2 access token from OAuth1 token and secret with API v2

SOLVED
1,368 Views
10 Comments
0 Kudos
Highlighted
Level 3

How to get OAuth2 access token from OAuth1 token and secret with API v2

All current users of our mobile app have OAuth1 token and secret stored for using their Dropbox account.  With the migration to API v2, we must use OAuth2 access tokens.  How can the OAuth1 token and secret be converted to an OAuth2 access token? The API v2 provides a route called /token/from_oauth1 which seems to provide a means to do this, but it requires an OAuth2 access token in order to work.  That makes no sense to me since it seems to require that you already have the answer in order to ask the question.  How can /token/from_oauth1 be used? And how can we convert our OAuth1 tokens and secrets to OAuth2 access tokens?

1 Accepted solution

Accepted Solutions
Solution
Dropboxer

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

No problem, that would look like this:

 

[DropboxClientsManager setupWithAppKey:@"APP_KEY"];

DBTransportClient *transportClient = [[DBTransportClient alloc] initWithAccessToken:nil
                                                                         selectUser:nil
                                                                          baseHosts:nil
                                                                          userAgent:nil
                                                                backgroundSessionId:nil
                                                                      delegateQueue:nil
                                                                             appKey:@"APP_KEY"
                                                                          appSecret:@"APP_SECRET"];
[DropboxClientsManager authorizedClient:[[DropboxClient alloc] initWithTransportClient:transportClient]];
[[DropboxClientsManager.authorizedClient.authRoutes tokenFromOauth1:@"OAUTH1_TOKEN_KEY" oauth1TokenSecret:@"OAUTH1_TOKEN_SECRET"] response:^(DBAUTHTokenFromOAuth1Result *result, DBAUTHTokenFromOAuth1Error *routeError, DBRequestError *error) {
    if (result) {
        NSLog(@"OAuth 2 access token: %@\n", result.oauth2Token);
    } else {
        NSLog(@"%@\n%@\n", routeError, error);
    }
}];
10 Replies
Dropboxer

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

That is the right way to do this, but it looks like the curl example in the documentation for /2/auth/token/from_oauth1 is unfortunately incorrect. I'll let the team know to fix that.

 

That endpoint actually uses app authentication, so an OAuth 2 access token isn't necessary. This is what the curl example should look like:

 

curl -X POST https://api.dropboxapi.com/2/auth/token/from_oauth1 \
    -u "APP_KEY:APP_SECRET" \
    --header "Content-Type: application/json" \
    --data "{\"oauth1_token\": \"OAUTH1_TOKEN_KEY\",\"oauth1_token_secret\": \"OAUTH1_TOKEN_SECRET\"}"
Level 3

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

Thanks, Greg. That's great. I drilled down to the HTTP API to try to get to the bottom of it, but I really should have asked about the Objective C API, since that's what I use. In that case it looks like I need to call a method on the AUTHRoute object, but I don't see a way to get one of those without a DropboxClient, which requires authentication. Can you give a quick example for Objective C? Thanks!
Solution
Dropboxer

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

No problem, that would look like this:

 

[DropboxClientsManager setupWithAppKey:@"APP_KEY"];

DBTransportClient *transportClient = [[DBTransportClient alloc] initWithAccessToken:nil
                                                                         selectUser:nil
                                                                          baseHosts:nil
                                                                          userAgent:nil
                                                                backgroundSessionId:nil
                                                                      delegateQueue:nil
                                                                             appKey:@"APP_KEY"
                                                                          appSecret:@"APP_SECRET"];
[DropboxClientsManager authorizedClient:[[DropboxClient alloc] initWithTransportClient:transportClient]];
[[DropboxClientsManager.authorizedClient.authRoutes tokenFromOauth1:@"OAUTH1_TOKEN_KEY" oauth1TokenSecret:@"OAUTH1_TOKEN_SECRET"] response:^(DBAUTHTokenFromOAuth1Result *result, DBAUTHTokenFromOAuth1Error *routeError, DBRequestError *error) {
    if (result) {
        NSLog(@"OAuth 2 access token: %@\n", result.oauth2Token);
    } else {
        NSLog(@"%@\n%@\n", routeError, error);
    }
}];
Level 3

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

That is great! Thanks!

Level 6

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

Can you point us to an example of using this with the new 3.0 SDK changes mentioned in https://github.com/dropbox/dropbox-sdk-obj-c/releases/tag/3.0.0 ?

Dropboxer

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

In the latest version of the SDK, the equivalent code would be:

 

DBTransportDefaultConfig *transportClient = [[DBTransportDefaultConfig alloc] initWithAppKey:@"APP_KEY" appSecret:@"APP_SECRET"];

[DBClientsManager setupWithTransportConfig:transportClient];
[[DBClientsManager.authorizedClient.authRoutes tokenFromOauth1:@"OAUTH1_TOKEN_KEY" oauth1TokenSecret:@"OAUTH1_TOKEN_SECRET"] setResponseBlock:^(DBAUTHTokenFromOAuth1Result *response, DBAUTHTokenFromOAuth1Error *routeError, DBRequestError * error) {
    if (response) {
        NSLog(@"OAuth 2 access token: %@\n", response.oauth2Token);
    } else {
        NSLog(@"%@\n%@\n", routeError, error);
    }
}];

 

If you are migrating from an old official SDK though, we recently added a helper that can automatically migrate those stored tokens:

 

https://github.com/dropbox/dropbox-sdk-obj-c#migrating-from-api-v1

Level 6

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

Thanks Greg, that last link helps a lot.

 

BTW, there's a typo in Migrating OAuth tokens from earlier SDKs, in the unsuccessfullyMigratedTokenData code. The format either has an extra %@ at the start or there is a missing argument.

      NSLog(@"%@DropboxUserID: %@, AccessToken: %@, AccessTokenSecret: %@, StoredAppKey: %@", tokenData[0],

 

Dropboxer

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

Thanks for letting us know, Steve. This should be updated now!

 

Let us know if you run into any issues migrating.

Level 2

Re: How to get OAuth2 access token from OAuth1 token and secret with API v2

Hi, I'm also working on migrating from API v1 to v2.  Do I have to migrate the existing tokens over in order for v2 to work?   Can I just set up v2 from scratch and then just ask my users to re-link to Dropbox when needed?

 

Thanks.