3
votes

When I used the core API I simply used the code

[dbsession updateAccessToken:@"..." accessTokenSecret:@"..." forUserId:@"..."];

to access my dropbox account from any copy of the app. But now I found out of this new Sync API that is easier and more flexible, but I didn't find any equivalent for the code displayed above. It now is:

DBAccountManager* accountMgr = [[DBAccountManager alloc] initWithAppKey:@"..." secret:@"..."];
[DBAccountManager setSharedManager:accountMgr];

??[DBAccountManager updateAccessToken:@"..." accessTokenSecret:@"..." forUserId:@"..."];??

How can I access my account? Where can I insert the AccessToken?

2
Just to clarify, you want to always login to the same dropbox account regardless of where the app is installed on? and by passing the dropbox login process? - djshiow

2 Answers

3
votes

From your question, it seems that this method on DBAccountManager is the one for using your appKey and secret:

- (id)initWithAppKey:(NSString *)key secret:(NSString *)secret

From the documentation description, it says this method "...create[s] a new account manager with your app’s app key and secret. You can register your app or find your key at the apps page."

After you create an instance of DBAccountManager and set it to be the shared manager using [DBAccountManager setSharedManager:], you can login the specific user by calling this method:

[[DBAccountManager sharedManager] linkFromController:YOUR_ROOT_CONTROLLER];

Here's a description from the dropbox iOS tutorial:

"To start interacting with the Sync API, you'll need to create a DBAccountManager object. This object lets you link to a Dropbox user's account which is the first step to working with data on their behalf"

"...the linking process will switch to the Dropbox mobile app if it's installed. Once the user completes the authorization step, Dropbox will redirect them back to your app using the URL scheme you registered when setting up the SDK. Your app needs to handle those requests to complete the auth flow."

The final step as mentioned in the tutorial is to handle the redirect. Here's some code to do this:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation {
    DBAccount *account = [[DBAccountManager sharedManager] handleOpenURL:url];
    if (account) {
        NSLog(@"App linked successfully!");
        return YES;
    }
}

The user's account information can now be obtained through [DBAccountManager sharedManager].linkedAccount which is a DBAccount with properties like userId and accountInfo.

Here's a link to the docs for reference. Hope this helps!

Update

It seems I may have misunderstood your question. I am giving you instructions on how to use the Sync API and didn't quite clarify that there is actually no place for a user's accessToken in the API. This has been replaced with the web flow that I describe above.

0
votes

You can achieve what you want by generating a callback url that dropbox uses in the sync API. First you need to set the dropbox.sync.nonce user setting to match whatever you pass in as the state parameter in the NSURL. Then set the oauth_token, oauth_token_secret, and uid params with what you used to pass into [DBAccountManager updateAccessToken:@"..." accessTokenSecret:@"..." forUserId:@"..."];. See below:

  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  [userDefaults setObject:@"9b0aa24b0bd50ce3a1a904db9d309c50"
                   forKey:@"dropbox.sync.nonce"];
  [userDefaults synchronize];

  NSURL *url =
      [NSURL URLWithString:@"db-APP_KEY://1/connect?
oauth_token=updateAccessToken&
oauth_token_secret=accessTokenSecret&
uid=forUserId&
state=9b0aa24b0bd50ce3a1a904db9d309c50"];    

  [[UIApplication sharedApplication] openURL:url];

Notice how the state parameter is the same as the value stored in the user defaults. Keep in mind this is undocumented and may change in a later API version.