Alessandro
Alessandro

Reputation: 4110

Dropbox OAuth token

Does anyone know how to use Auth tokens for dropbox in objective c iPad/iphone developing on Xcode?? I have searched many tutorials and the closest I have found is this:

http://code.google.com/p/oauthconsumer/wiki/UsingOAuthConsumer

but it is for mac. What I want to do is to link all the app users to the same dropbox account (mine) without displaying the safari window for signing in.

Any ideas??

Upvotes: 2

Views: 1835

Answers (1)

Joony
Joony

Reputation: 4718

This is possible, but not recommended.

WARNING: This would allow anyone access to read/write to your dropbox account (or App folder, depending on what access you allow).

I presume you are setting your App up as recommended in the Getting Started Guide: https://www.dropbox.com/developers/core/authentication#ios

Here's how it works: once you login to Dropbox, you are re-directed back to your app. Dropbox does this by getting you to register a URL Scheme and making use of the following AppDelegate method:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;

Dropbox passes the oauth_token, oauth_token_secret, and uid in the url parameter. It then saves these for later use when you make API calls by using the following method of DBSession:

- (BOOL)handleOpenURL:(NSURL *)url;

So what you can do it create an app that uses the same App Key and Secret.

TEST APP

DBSession* dbSession =
    [[[DBSession alloc]
      initWithAppKey:@"APP_KEY"
      appSecret:@"APP_SECRET"
      root:ACCESS_TYPE] // either kDBRootAppFolder or kDBRootDropbox
     autorelease];
[DBSession setSharedSession:dbSession];

Request authorization

if (![[DBSession sharedSession] isLinked]) {
    [[DBSession sharedSession] linkFromController:yourRootController];
}

And then add the AppDelegate method to receive the authorization url

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if ([[DBSession sharedSession] handleOpenURL:url]) {
        if ([[DBSession sharedSession] isLinked]) {
            NSLog(@"App linked successfully! url: %@", url);
            // At this point you can start making API calls
        }
        return YES;
    }
    // Add whatever other url handling code your app requires here
    return NO;
}

This will log the url with the authorization token. Copy that, then in your production app (right after you configure your DBSession) just do this (replacing the string with the one you previously copied):

PRODUCTION APP

DBSession* dbSession =
    [[[DBSession alloc]
      initWithAppKey:@"APP_KEY"
      appSecret:@"APP_SECRET"
      root:ACCESS_TYPE] // either kDBRootAppFolder or kDBRootDropbox
     autorelease];
[DBSession setSharedSession:dbSession];

if (![[DBSession sharedSession] isLinked]) {
    [[DBSession sharedSession] handleOpenURL:[NSURL URLWithString:@"db-APP_KEY://1/connect?oauth_token=********&oauth_token_secret=********&uid=********"]];
}

This will automatically link the DBSession to your Dropbox account.

You can test this by calling this just before you authorize:

[[DBSession sharedSession] unlinkAll];

ANOTHER WARNING I could just download your app, extract the authorization token, and then start making all the read/write API calls I want. This is completely insecure and should only be regarded as an educational exercise.

Upvotes: 3

Related Questions