【问题标题】:Upload tracks using SoundCloud API on iOS在 iOS 上使用 SoundCloud API 上传曲目
【发布时间】:2014-12-08 17:11:07
【问题描述】:

我必须使用 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 是一个数组,其中包含我帐户中所有曲目的列表。没有错误,但没有上传文件。模拟器和设备得到相同的响应。 欢迎任何建议。

附:我也尝试过像这样更改正文和内容类型的发布请求,但没有工作结果:

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"];

【问题讨论】:

    标签: ios audio file-upload httprequest soundcloud


    【解决方案1】:

    我正在做类似的事情,我得到了代码 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]];
    

    你有没有得到任何地方?

    【讨论】:

      【解决方案2】:

      我现在有了用 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-26
        相关资源
        最近更新 更多