我正在使用 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"}
我不知道为什么会出现这个问题,请帮助我。