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
Ghulam A.
9 years agoNew member | Level 1
Is linking necessary to download files?
Is it possible to download files from my app folder (Dropbox) to my app without linking an account? If so, how would I do that?
- Greg-DBDropbox Staff
Are you using an SDK or library? If so, which?
- Ghulam A.New member | Level 1
I'm using the sdk following the tutorial on https://www.dropbox.com/developers-v1/core/start/ios which I now see has v1 in the URL but if you could explain that still, that would be great.
- Greg-DBDropbox Staff
That SDK uses OAuth 1 and version 1 of the Dropbox API, a.k.a. the Core API. The DBRestClient.loadAccountInfo method corresponds to /account/info.
- Ghulam A.New member | Level 1
Okay so I'd put this somewhere:
DBRestClient.loadAccountInfo;
How would I view/retrieve the resulting info?
- Ghulam A.New member | Level 1
What is the corresponding function to get the access token secret? And is the access token you can generate from the account page the correct one to use with V1 or is it only for V2? (It doesn't give a token secret.)
- Greg-DBDropbox Staff
I recommend working through the tutorial to see how to call methods in that SDK. For example:
[self.restClient loadMetadata:@"/"];
So, using loadAccountInfo would look something like:
[self.restClient loadAccountInfo];
And you'd need to implement these delegate methods to get the response:
- (void)restClient:(DBRestClient*)client loadedAccountInfo:(DBAccountInfo*)info;
- (void)restClient:(DBRestClient*)client loadAccountInfoFailedWithError:(NSError*)error;The App Console only lets you generate an OAuth 2 access token, so that wouldn't suit your needs, as you need an OAuth 1 access token. You can get that by implementing the app authorization flow per the tutorial just to use for your account, and then pulling the access token out using DBSession.credentialStoreForUserId. (Again though, I should emphasize that this is not a recommended way of using the API, which is why this isn't particularly easy. If you just need to distribute content to the users of your app, a CDN might be a better solution.)
- Ghulam A.New member | Level 1
I figured out how to get the user id. All I need is the access token with secret and I'll be set. I'm not sure where to implement DBSession.credentialStoreForUserID though. I'm doing this within the example DBRoulette Xcode project. Would I call it like this?
[[DBSession sharedSession] credentialStoreForUserID];
Where would I put that and how would I retrieve the value for the access token? The most I can find online is this in terms of getting a value and I don't know where I would put that either.
DBSession.sharedSession().credentialStoreForUserId(userId).accessToken
If you could use the DBRoulette project for reference, that would help a lot.
By the way, thank you for sticking with me. I understand that this is not intended use, but I still wish the documentation was more straightforward. I don't see anything about access tokens in the tutorial you linked. I did follow that earlier for setting up my actual app.
- Greg-DBDropbox Staff
You just need to call credentialStoreForUserID once to get your access token/secret, which you can do anywhere in the app, as long as the account is already linked. That would look like:
MPOAuthCredentialConcreteStore *creds = [[DBSession sharedSession] credentialStoreForUserId:@"12345"];
NSLog(@"access token key: %@ secret: %@", creds.accessToken, creds.accessTokenSecret);(Where 12345 is your user ID.)
- Ghulam A.New member | Level 1
Okay, that did allow me to get an access token and secret. However, I still don't seem to be able to retrieve a file from my folder using this method. If I login normally to link my dropbox, it does work. (I basically pull a text file from my Dropbox and display the text.) I'm not sure what I'm doing wrong here. The one thing I did notice was that I get a new access token and secret every time I login normally. Do the tokens expire? Anyways, here is my code cutting out irrelevant portions and replacing sensitive info:
In my app delegate:
.h
#import <UIKit/UIKit.h>
@interface AppDelegate : NSObject <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end.m
#import "AppDelegate.h"
#import <DropboxSDK/DropboxSDK.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
DBSession *dbSession = [[DBSession alloc]
initWithAppKey:@"key"
appSecret:@"secret"
root:kDBRootAppFolder]; // either kDBRootAppFolder or kDBRootDropbox
[DBSession setSharedSession:dbSession];
//[[DBSession sharedSession] unlinkAll];
[dbSession updateAccessToken:@"token" accessTokenSecret:@"token secret" forUserId:@"id number"];
}
@endIn my view controller:
.h file
#import <UIKit/UIKit.h>
@class DBRestClient;
@interface homeVC : UIViewController
{
DBRestClient *restClient;
}
@end.m file
#import "homeVC.h"
#import <DropboxSDK/DropboxSDK.h>
@interface homeVC () <UITableViewDataSource, UIAccelerometerDelegate, DBRestClientDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) DBRestClient *restClient;
@end
@implementation homeVC
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path2 = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@", @"Welcome.txt"]];
[self.restClient loadMetadata:@"/"];
[self.restClient loadFile:@"/Welcome.txt" intoPath:path2];
NSString* content = [NSString stringWithContentsOfFile:path2
encoding:NSUTF8StringEncoding
error:NULL];
NSLog(@"%@",content);
} - Greg-DBDropbox Staff
Tokens don't expire by themselves (but they can be manually revoked).
What doesn't work exactly? What error or output are you getting?
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,880 PostsLatest Activity: 49 minutes agoIf 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!