How to add comments through the SoundCloud API

怎甘沉沦 提交于 2019-12-24 01:06:16

问题


I would like to post comments via the SoundCloud API but looking at the documentation it seems impossible, the only method available for /tracks/{id}/comments seems to be GET. When I try to send it a POST I get a HTTP Error: 422, same happens if I use the console I get a 422 too, with "error_message": "Body can't be blank" even so I added body=test as a parameter.

Any idea how you are supposed to add a comments via the API?

I can see that it seems to be possible with for example the ruby SDK:

# create a new timed comment
comment = client.post("/tracks/#{track.id}/comments", :comment => {
  :body => 'This is a timed comment',
  :timestamp => 1500
})

but I am using the Objective-C SDK and this is my code so far (which returns a 422):

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:content forKey:@"body"];
if (timestamp >= 0)
{
    [dict setObject:[NSString stringWithFormat:@"%i", timestamp] forKey:@"timestamp"];
}

NSString *resource = [NSString stringWithFormat:@"tracks/%i/comments", trackId];

[scAPI performMethod:@"POST" onResource:resource withParameters:dict context:delegate userInfo:nil];

回答1:


Ok, I was able to find it out on myself, I had a look at the Java version and its examples:

List<NameValuePair> params = new java.util.ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("comment[body]", "This is a test comment"));

HttpResponse response = api.post("tracks/" + TRACK_ID + "/comments", params);

So the strange decision to not only have bodyas the key but comment[body] and without documenting it in the API was what I had problems with. I tried to do it like the ruby version suggested do have a NSDictionary with the key comment and within another one with body and timestamp but that would just cast errors because the SDK didn't expect nested dictionaries. So now the code looks like this:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:content forKey:@"comment[body]"];
if (timestamp >= 0)
{
    [dict setObject:[NSString stringWithFormat:@"%i", timestamp] forKey:@"comment[timestamp]"];
}

NSString *resource = [NSString stringWithFormat:@"tracks/%i/comments", trackId];

[scAPI performMethod:@"POST" onResource:resource withParameters:dict context:delegate userInfo:nil];



回答2:


Here is a great music app which allows you to connect favorably with Amazon's latest music, videos, and movies while freelancing other products as well.



来源:https://stackoverflow.com/questions/10768474/how-to-add-comments-through-the-soundcloud-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!