I am getting error 400 while trying to retrieve the users full list of files name from the app folder after the user logs in.
I am using this tutorial to set up the the dropbox sdk: https://www.dropbox.com/developers-v1/core/start/ios
but changed the approach a little bit as not all the action is taking place in my app delegate .m file.
Also I had an error after first running the app even though I was doing it as in the tutorial, but after a quick search I found out that I had to add the next lines in my Info.plis for everything to run: LSApplicationQueriesSchemes dbapi-2
after doing so I am able to successfully authenticate to dropbox and after I do that I am sending a request to a shared session custom class that hold the rest of my Dropbox functionality and try to get the list of files from inside the app folder, then I get the next error:
App linked successfully!
[WARNING] DropboxSDK: error making request to /1/metadata/sandbox - (400) invalid_request
Error loading metadata: Error Domain=dropbox.com Code=400 "(null)" UserInfo={error_description=No auth function available for given request, path=/, error=invalid_request}
After I get this error if I put the same code in the viewWillAppear or the viewDidLoad of the same class then the code is executed and it returns and empty dictionary witch means that there are no photos in my app folder and I can proceed with my files upload.
My question is: why do I have to restart the app in order for the same call to work and why isn't it working from the start?
Now for my settings: 1. when I created the app on the dropbox app platform I choosed: - API : Dropbox API not dropbox for business api - permisions: APP Folder not Full Dropbox 2. in the info.plis file I added the next values:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>dbapi-2</string>
</array>
AND:
Now in my App delegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
.........
[self initDropbox];
return YES;
}
- (void)initDropbox{
DBSession *dbSession = [[DBSession alloc]
initWithAppKey:@"appKey"
appSecret:@"appSecret"
root:kDBRootAppFolder];
[DBSession setSharedSession:dbSession];
}
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
sourceApplication:(NSString *)source
annotation:(id)annotation {
if ([[DBSession sharedSession] handleOpenURL:url]) {
if ([[DBSession sharedSession] isLinked]) {
NSLog(@"App linked successfully!");
// At this point you can start making API calls
[[NSNotificationCenter defaultCenter] postNotificationName:@"applicationDidLinkWithDropbox"
object:self];
}
return YES;
}
// Add whatever other url handling code your app requires here
return NO;
}
And in my view controller I have:
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dropBoxDidLink:)
name:@"applicationDidLinkWithDropbox"
object:nil];
//method that gets called after authentication or when the screen is displayed
//the methods is called only is it if not running already, I check if it is running with the isBussy bool
DropBoxSuport *managedObj = [DropBoxSuport sharedInstance];
BOOL isBussy = managedObj.isUploadingImage;
if ([managedObj isAuthenticated] && !isBussy) {
managedObj.isSingleUpload = NO;
[managedObj startDropboxBackup];
}
}
- (IBAction)didTappedDropboxButton:(id)sender {
if (![[DBSession sharedSession] isLinked]) {
[[DBSession sharedSession] linkFromController:self];
}else{
[[DBSession sharedSession] unlinkAll];
dropDownView.firstCellSwitch.on = NO;
}
}
- (void) dropBoxDidLink:(NSNotification *)notification {
if ([[notification name] isEqualToString:@"applicationDidLinkWithDropbox"]) {
dropDownView.firstCellSwitch.on = YES;
dropDownView.firstCellSwitch.userInteractionEnabled = NO;
DropBoxSuport *managedObj = [DropBoxSuport sharedInstance];
BOOL isBussy = managedObj.isUploadingImage;
if ([managedObj isAuthenticated] && !isBussy) {
managedObj.isSingleUpload = NO;
[managedObj startDropboxBackup];
}
}
}
And in my custom class(DropBoxSuport.m):
+(instancetype) sharedInstance {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[super alloc] initUniqueInstance];
[shared initializeRestClient];
});
return shared;
}
-(instancetype) initUniqueInstance {
return [super init];
}
- (void)initializeRestClient{
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
context = [self appDelegate].managedObjectContext;
}
- (void)startDropboxBackup{
if (!isUploadingImage && [self appDelegate].checkForNetwork && [self isAuthenticated]) {
isUploadingImage = YES;
[restClient loadMetadata:@"/"];
}
}
#pragma mark - dropbox delegate get file names
- (void)restClient:(DBRestClient *)client
loadedMetadata:(DBMetadata *)metadata {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
if (metadata.isDirectory) {
NSLog(@"Folder '%@' contains:", metadata.path);
for (DBMetadata *file in metadata.contents) {
if (file.filename) {
[tempArray addObject:file.filename];
}
}
}
NSArray *localStoredImages = [[NSArray alloc] initWithArray:[self loadAllCoreDataImagePaths]];
NSArray *dropboxStoredImages = [[NSArray alloc] initWithArray:tempArray];
differenceArray = [self getDifferenceBetweenArray:localStoredImages andSecondArray:dropboxStoredImages];
if (differenceArray && differenceArray.count > 0) {
[self startDropboxUpload];
}else{
isUploadingImage = NO;
errorCount = 0;
currentImageUploaded = 0;
}
}
- (void)restClient:(DBRestClient *)client
loadMetadataFailedWithError:(NSError *)error {
NSLog(@"Error loading metadata: %@", error);
isUploadingImage = NO;
errorCount = 0;
currentImageUploaded = 0;
}
So pretty much this is my code and how I use it, but as I sad the code only happens when the user authenticates for the first time and I try to get the folder names. Also tried to get the folder later without restarting the app and the same error occurred. But if I restart the app everything works fine.
Any help is much appreciated
