node-soundcloud icon indicating copy to clipboard operation
node-soundcloud copied to clipboard

401 - Unauthorized on POST request to '/tracks' route

Open tryenc opened this issue 9 years ago • 6 comments

I'm getting a 401 - Unauthorized on POST request to the '/tracks' route. I'm trying to upload a track to my Soundcloud profile. Any idea why this wouldn't be working? Thanks so much in advance!

        var SC = require('node-soundcloud');

        SC.init({
            id: client_id, 
            secret: client_secret, 
            uri: redirect_uri,
            accessToken: access_token
        });

        var mp3Path = path.join(__dirname + '../../../test2.mp3')

        SC.post('/tracks', {
            track: {
                'asset_data': fs.createReadStream(mp3Path),
                'title': '4.20-14.47'
            }
        }, function(err, data){
            console.log("data", data);
            res.end();
        })

tryenc avatar Apr 20 '16 18:04 tryenc

According to their API documentation, the key for the actual file should be file and not asset_data: https://developers.soundcloud.com/docs/api/guide#uploading

Is there a reason you're using asset_data?

jakemmarsh avatar Apr 20 '16 19:04 jakemmarsh

Hey Jake,

Thanks for getting back so fast. I was using 'asset_data' because that's what the API Reference says to use for a request to '/tracks'. The 'asset_data' property is at the very bottom of the '/tracks' section. Regardless, I tried issuing the same post using 'file' in place of 'asset_data' and I got the same error.

Thanks again for the fast response. I'm really looking forward to getting this solved.

tryenc avatar Apr 20 '16 19:04 tryenc

Ah, okay. How are you retrieving your access_token?

jakemmarsh avatar Apr 20 '16 19:04 jakemmarsh

Utilizing the JavaScript SDK inside of an Angular controller. The SDK is requested in a script tag in the HTML. If I upload with the SDK from the front-end it's working.

app.controller('SoundcloudController', ['$scope', 'SoundcloudService', function($scope, SoundcloudService){

  SC.initialize({
    client_id: 'somefakecilentid',
    redirect_uri: 'http://localhost:1337/callback.html',
    display: 'popup'
  });

  $scope.song = {};

  $scope.upload = function(){ 

    var fd = new FormData();
    fd.append('file', $scope.song.file);
    fd.append('title', $scope.song.title);

    SC.connect({
      scope: 'non-expiring',
      response_type: 'token'
    })
//================ UPLOAD FROM FRONT-END ================

    // .then(function(){
  
    //   return SC.upload({
    //     file: $scope.song.file,
    //     title: $scope.song.title
    //   })
    // })
    // .then(function(track){
    //   console.log("track", track);
    // })

//========== UPLOAD FROM BACK-END WITH SERVICE ==========

    .then(function(res){
      console.log("res.oauth_token", res.oauth_token);
      return fd.append('accessToken', res.oauth_token);
    })
    .then(function(){  
      return SoundcloudService.uploadFile(fd);
    })   
    .then(function(res){
      console.log("res", res);
    })

  };

}]);

tryenc avatar Apr 20 '16 20:04 tryenc

Hey Guys!

I'm getting the same error and I'm following the same method which Tryenc has used.. is there any resolution for this?

Thank you!

akatrodiya avatar May 27 '16 11:05 akatrodiya

I'm running into something similar too, I'm using the connect screen authentication to get a token but any subsequent requests return a 401.

TheMSB avatar Sep 14 '16 09:09 TheMSB