0

我正在使用 DropBox 核心 api OAuth 2.0 身份验证,我正在尝试通过它的核心 api 上传 UIImage

https://api-content.dropbox.com/1/files// _

https://www.dropbox.com/developers/core/docs ( /files (POST) )

我的代码是

-(void)uploadData:(NSData *)imageAsData and:(NSString *)fileName{

NSString *urlString=[NSString stringWithFormat:@"https://api-content.dropbox.com/1/files/dropbox/Apps?access_token=%@",dropBoxToken];


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];


NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];


NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"attachement\"; filename=\"%@\";",fileName] dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageAsData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];




NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"returnString=%@",returnString);

其中图像和文件名的 NSData 中的 imageAsData 是“abc.jpg”。

returnString 的输出是 {"error": "file parameter value 'None' is invalid"}

我不知道为什么会出现这个问题,请帮助我。

4

1 回答 1

1

请为自己省去很多麻烦,并改用更简单的files_put端点。(在文档中推荐它是有原因的。)

粗略编辑您的代码(未经测试):

-(void)uploadData:(NSData *)imageAsData and:(NSString *)filePath {

    NSString *urlString=[NSString stringWithFormat:
        @"https://api-content.dropbox.com/1/files_put/dropbox/%@?access_token=%@",
        filePath, dropBoxToken];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"PUT"];

    [request setHTTPBody:imageAsData];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnString=%@",returnString);
}
于 2013-11-03T18:21:30.013 回答