[英]Upload tracks using SoundCloud API on iOS
我必须使用HTTP方法将mp3文件上传到SoundCloud。 我没有收到任何错误,但是文件未显示在SoundCloud上。
我正在使用multipart/form-data request
这是我当前的处理方式:(我已经完成了连接,授权并且知道我的用户ID)。
NSString *url=[NSString stringWithFormat:@"http://api.soundcloud.com/tracks.json?client_id=%@&user_id=%@&title=%@&tag_list=%@&sharing=%@&oauth_token=%@",SOUNDCLOUD_CLIENT_ID, inputData[@"user_id"],inputData[@"title"],inputData[@"tag_list”],inputData[@"sharing”], ACCESS_TOKEN];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
request.HTTPMethod = @“POST”;
NSString *boundary= @“SoundCloud”;
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\n--%@\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\”asset_data\"; filename=\"%@\"\n”,@“my file name.mp3"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: audio/mpeg\n\n” dataUsingEncoding:NSUTF8StringEncoding];
NSData *audioData=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"untitled" ofType:@"mp3”]];
[body appendData:[NSData dataWithData:audioData]];
[body appendData:[[NSString stringWithFormat:@"\n--%@", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
request.HTTPBody = body;
[request setValue:[NSString stringWithFormat:@"%lud",(unsigned long)[body length]] forHTTPHeaderField:@"Content-Length”];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@,boundary] forHTTPHeaderField:@"content-Type"];
request.timeoutInterval = 30;
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.connection start];
我实际上是在.mp3
文件中搜索的,因此“ audioData”具有正确的内容。 我收到200 HTTP响应代码:
NSHTTPURLResponse:{ URL: https://api.soundcloud.com/tracks.json?client_id=[...]&user_id=[...]&title=dgd&tag_list=wgn&sharing=public&oauth_token=[...] } { status code: 200, headers {
"Access-Control-Allow-Headers" = "Accept, Authorization, Content-Type, Origin";
"Access-Control-Allow-Methods" = "GET, PUT, POST, DELETE";
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = Date;
"Cache-Control" = "private, max-age=0, must-revalidate";
"Content-Encoding" = gzip;
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 08 Dec 2014 15:58:57 GMT";
Etag = "\"66cc86baea454001c9572594b9caeea0\"";
Server = "am/2";
"Set-Cookie" = "_session_auth_key=\"LdrvaLfDKfaGihR714EaihOotlo=\"";
"Transfer-Encoding" = Identity;
} }
然后,响应json是一个数组,其中包含我帐户中所有曲目的列表。 没有错误,但没有文件上传。 模拟器和设备可以获得相同类型的响应。 欢迎任何建议。
PS我也尝试过发布请求,以这种方式更改正文和内容类型,但是没有任何工作结果:
NSDictionary *body=@{@"asset_data":[[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"untitled" ofType:@"mp3"]]base64EncodedStringWithOptions:0]};
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
request.HTTPBody = bodyData;
[request addValue:@"application/json" forHTTPHeaderField:@"content-Type"];
我正在做类似的事情,并且正在获取代码400,但是我还包括了一个访问令牌,我认为您需要这样做。
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"oauth_token\"\r\n\r\n%@", self.scToken] dataUsingEncoding:NSUTF8StringEncoding]];
你有没有得到这个?
我现在有使用NSURLRequest上传歌曲的工作代码。 我希望这可以帮助你。
-(void)uploadTrackTitled:(NSString*)title withPath:(NSString*)trackPath {
NSURLSession* urlSession = [NSURLSession sharedSession];
NSURLRequest* urlRequest = [self getURLRequest:title withAudioPath:trackPath];
NSURLSessionDataTask* dataTask = [urlSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"returned: %ld", (long)httpResponse.statusCode);
if(data) {
NSString* stringResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", stringResponse);
}
if(error) {
NSLog(@"%@", [error localizedDescription]);
}
}];
[dataTask resume];
}
-(NSURLRequest*)getURLRequest:(NSString*)title withAudioPath:(NSString*)audioPath {
CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString* boundary = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.soundcloud.com/tracks"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[self getPostData:self.scToken withBoundary:boundary andTitle:title withAudioPath:audioPath]];
NSString* contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
return request;
}
-(NSData*)getPostData:(NSString*)token withBoundary:(NSString*)boundary andTitle:(NSString*)title withAudioPath:(NSString*)audioPath {
NSString* boundaryStart = [NSString stringWithFormat:@"--%@\r\n", boundary];
NSString* boundaryEnd = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
NSMutableData* bodyData = [[NSMutableData alloc] init];
//add the token
NSMutableString* tokenSection = [NSMutableString stringWithString:boundaryStart];
[tokenSection appendFormat:@"Content-Disposition: form-data; name=\"oauth_token\"\r\n\r\n"];
[tokenSection appendFormat:@"%@\r\n", token];
[bodyData appendData:[tokenSection dataUsingEncoding:NSUTF8StringEncoding]];
//track title
NSMutableString* titleSection = [NSMutableString stringWithString:boundaryStart];
[titleSection appendFormat:@"Content-Disposition: form-data; name=\"track[title]\"\r\n\r\n"];
[titleSection appendFormat:@"%@\r\n", title];
[bodyData appendData:[titleSection dataUsingEncoding:NSUTF8StringEncoding]];
//add the audio file
NSData* trackData = [NSData dataWithContentsOfFile:audioPath];
NSMutableString* trackSection = [NSMutableString stringWithString:boundaryStart];
[trackSection appendFormat:@"Content-Disposition: form-data; name=\"track[asset_data]\"; "];
[trackSection appendFormat:@"filename=\"%@\"\r\n", [audioPath lastPathComponent]];
[trackSection appendFormat:@"Content-Type: application/octet-stream\r\n\r\n"];
[bodyData appendData:[trackSection dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:trackData];
//ending
[bodyData appendData:[boundaryEnd dataUsingEncoding:NSUTF8StringEncoding]];
return bodyData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.