如何通过 SoundCloud API 添加评论 [英] How to add comments through the SoundCloud API
问题描述
我想通过 SoundCloud API 发表评论,但查看 文档 似乎不可能,/tracks/{id}/comments
唯一可用的方法似乎是 GET
.当我尝试向它发送 POST
时,我收到一个 HTTP 错误:422
,如果我使用控制台也会发生同样的情况,我也收到一个 422,带有 "error_message": "body can't be blank"
即使我添加了 body=test
作为参数.
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.
知道如何通过 API 添加评论吗?
Any idea how you are supposed to add a comments via the API?
我可以看到,例如使用 ruby SDK 似乎是可能的:
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
})
但我使用的是 Objective-C SDK,这是我目前的代码(返回 422):
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];
推荐答案
好吧,我自己找到了,我查看了 Java 版本及其示例:
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);
所以奇怪的决定不仅有 body
作为键而且 comment[body]
并且没有在 API 中记录它是我遇到的问题.我试着这样做,就像建议的 ruby 版本有一个 NSDictionary
与键 comment
和另一个带有 body
和 timestamp 的
但这只会抛出错误,因为 SDK 不期望嵌套字典.所以现在代码看起来像这样:
So the strange decision to not only have body
as 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];
这篇关于如何通过 SoundCloud API 添加评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!