ios - 加载用户文件的所有路径时,Dropbox 错误 400 invalid_request

标签 ios objective-c dropbox-api

我在用户登录后尝试从应用程序文件夹中检索用户的完整文件名列表时遇到错误 400。

我正在使用本教程来设置 Dropbox SDK: https://www.dropbox.com/developers-v1/core/start/ios

但由于并非所有操作都发生在我的应用程序委托(delegate) .m 文件中,所以稍微改变了方法。

此外,我在第一次运行该应用程序后遇到了错误,即使我是按照教程中的那样进行操作,但经过快速搜索后我发现我必须在我的 Info.plis 中添加下一行才能运行所有内容: LSApplicationQueriesSchemes dbapi-2

这样做之后,我能够成功地向 Dropbox 进行身份验证,然后我将请求发送到共享 session 自定义类,该自定义类包含我的其余 Dropbox 功能,并尝试从应用程序内部获取文件列表文件夹,然后我得到下一个错误:

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}

出现此错误后,如果我将相同的代码放入同一类的 viewWillAppear 或 viewDidLoad 中,则会执行代码并返回,空字典女巫意味着我的应用程序文件夹中没有照片,我可以继续上传我的文件。

我的问题是: 为什么我必须重新启动应用程序才能使同一个调用起作用,为什么它从一开始就不起作用?

现在进行我的设置: 1. 当我在我选择的 dropbox 应用平台上创建应用时: - API:Dropbox API 不是 dropbox for business api - 权限:APP 文件夹不是完整的 Dropbox 2. 在 info.plis 文件中,我添加了下一个值:

 <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>dbapi-2</string>
    </array>

和:

enter image description here

现在在我的 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;
}

在我的 View Controller 中我有:

- (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];
        }
    }
}

在我的自定义类(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;
}

这就是我的代码以及我的使用方式,但令我遗憾的是,代码仅在用户首次进行身份验证并且我尝试获取文件夹名称时才会出现。稍后还尝试在不重新启动应用程序的情况下获取文件夹,但发生了同样的错误。但如果我重新启动应用程序,一切正常。

非常感谢任何帮助

最佳答案

相信这是一个时间问题。 sharedSession 获得授权后,您需要初始化一个新的 DBRestClient

尝试添加一些日志记录,以查看何时相对于链接后更新的 sharedSession 调用 sharedInstance

关于ios - 加载用户文件的所有路径时,Dropbox 错误 400 invalid_request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33079190/

相关文章:

ios - 可以按 iPhone 设备类型分割结构使用情况吗?

ios - 颜色属性在 UITextField 中与 NSAttributedString 保持一致

ios - 将数据从 plist 添加到 NSSet?

iphone - NSURLConnection 的 Xcode 4 警告 "Expression result unused”

python - 以编程方式从共享 Dropbox 文件夹链接下载内容

ios - 安装Youtube iOS应用后,Metarefresh失败

ios - Swift 2 无代表

ios - 如何使用 include_media_info 获取元数据?

ios - 文件管理器有时在文档目录中找不到子文件夹

java - 使用 DropboxAPI 进行 OAuth,无需手动复制 url 进行授权