chmod
chmod

Reputation: 1253

SoundCloud API iOS Fails to upload audio with a HTTP 422 error

I'm trying to put a super basic Soundcloud feature in my app to upload single .wav files using their UI. I followed their guide and I didn't really need anything outside of the bare bones share menu so I assumed this code would work:

              NSURL *trackURL = [[NSURL alloc] initWithString:[docsDir stringByAppendingPathComponent:fileToShare]];

            SCShareViewController *shareViewController;
            shareViewController = [SCShareViewController shareViewControllerWithFileURL:trackURL
                                                                      completionHandler:^(NSDictionary *trackInfo, NSError *error){

                                                                          if (SC_CANCELED(error)) {
                                                                              NSLog(@"Canceled!");
                                                                          } else if (error) {
                                                                              NSLog(@"Ooops, something went wrong: %@", [error localizedDescription
                                                                                                                         ]);
                                                                          } else {
                                                                              // If you want to do something with the uploaded
                                                                              // track this is the right place for that.
                                                                              NSLog(@"Uploaded track: %@", trackInfo);
                                                                          }
                                                                      }];

            // If your app is a registered foursquare app, you can set the client id and secret.
            // The user will then see a place picker where a location can be selected.
            // If you don't set them, the user sees a plain plain text filed for the place.
            [shareViewController setFoursquareClientID:@"<foursquare client id>"
                                          clientSecret:@"<foursquare client secret>"];

            // We can preset the title ...
            [shareViewController setTitle:@"Funny sounds"];


            // ... and other options like the private flag.
            [shareViewController setPrivate:NO];

            // Now present the share view controller.
            [self presentModalViewController:shareViewController animated:YES];

            [trackURL release];

However I get an HTTP 422 error with this appearing in my debug console:

2016-02-29 11:04:47.129 synthQ[801:443840] parameters: {
    "track[asset_data]" = "/var/mobile/Containers/Data/Application/EE58E5CA-B30C-44EB-B207-EB3368263319/Documents/bb.wav";
    "track[downloadable]" = 1;
    "track[post_to][]" = "";
    "track[sharing]" = public;
    "track[tag_list]" = "\"soundcloud:source=synthQ\"";
    "track[title]" = "Funny sounds";
    "track[track_type]" = recording;
}
2016-02-29 11:04:47.164 synthQ[801:444011] -[NXOAuth2PostBodyStream open] Stream has been reopened after close
2016-02-29 11:04:47.373 synthQ[801:443840] Upload failed with error: HTTP Error: 422 Error Domain=NXOAuth2HTTPErrorDomain Code=422 "HTTP Error: 422" UserInfo={NSLocalizedDescription=HTTP Error: 422}

Does anyone have any ideas what could be going wrong here?

Thanks!

Upvotes: 1

Views: 3512

Answers (3)

eracube
eracube

Reputation: 2639

One of the reason to get HTTP 422 error in SoundCloud is:

If you are trying to upload a file for a first time with a new account, you need to verify your email address to complete your SoundCloud registration in order to upload your files.

There might be other reasons for this error, however for my case it was the case and that solved the problem.

Upvotes: 1

chmod
chmod

Reputation: 1253

I managed to solve this by using a different constructor that passes the audio file as an NSData object:

shareViewController = [SCShareViewController shareViewControllerWithFileData:[NSData dataWithContentsOfFile:[docsDir stringByAppendingPathComponent:fileToShare]]
                                                                          completionHandler:^(NSDictionary *trackInfo, NSError *error){

                                                                              if (SC_CANCELED(error)) {
                                                                                  NSLog(@"Canceled!");
                                                                              } else if (error) {
                                                                                  NSLog(@"Ooops, something went wrong: %@", [error localizedDescription
                                                                                                                             ]);
                                                                              } else {
                                                                                  // If you want to do something with the uploaded
                                                                                  // track this is the right place for that.
                                                                                  NSLog(@"Uploaded track: %@", trackInfo);
                                                                              }
                                                                          }];

Upvotes: 0

Milap Kundalia
Milap Kundalia

Reputation: 1606

You cannot reference external resources while uploading tracks. You therefor need to download the track to your computer and then perform the actual upload to SoundCloud.

Source Soundcloud file uploading issue HTTP 442

Upvotes: 0

Related Questions