[UPDATE] The problem is fixed. Working code and method updated below!
So, I'm trying to post a comment on the SoundCloud track using the SoundCloud api in AS3. The documentation can be found here:
http://developers.soundcloud.com/docs/#comments
And my code is as follows:
var urlLoader:URLLoader = new URLLoader(); var urlString:String = "https://api.soundcloud.com/tracks/" + soundModel.currentlyPlayingItem.id + "/comments.json" + "?comment[body]=" + commentString + "&comment[timestamp]=" + trackPositionComment; var urlRequest:URLRequest = new URLRequest(urlString); urlRequest.method = URLRequestMethod.POST; var variables:URLVariables = new URLVariables(); variables.oauth_token = soundModel.userToken; //here is the filler variable to make POST, not GET. urlRequest.data = variables; urlRequest.contentType = "application/x-www-form-urlencoded"; var header2:URLRequestHeader=new URLRequestHeader("oauth_token",soundModel.userToken); var header3:URLRequestHeader=new URLRequestHeader("client_id", "sdjhb4jhbkjbe4gkjbh4random"); urlRequest.requestHeaders.push(header2); urlRequest.requestHeaders.push(header3); urlLoader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, commentPosted); urlLoader.load(urlRequest);
Initially, I was constantly getting error 401 - unauthorized.
After playing with the SoundCloud API console, I saw that there was a “content length” of 0, not a body. Thus, all authentication materials should have been in the headers and not in the variables. However, using the POST method in AS3 UrlLoader, it is automatically sent as GET if there is no body (variables) in it. Therefore, for this reason, I left an arbitrary value for the variable.
Also, the two parameters, “body” and “timestamp”, should indeed be sent as “comment [body]” and “comment [timestamp]”, although this is not clearly indicated in the documentation. Also, both of these parameters should be included in the URL, and not as variables or headers.
Finally, in order to receive an authorized return, you must provide both your "client_id" and "oauth_token" as headers.
It was this magical combination that did it for me. Hope this helps some other people willing to rip out their hair like me.
thank you Theo M.