jakemmarsh / node-soundcloud

node.js wrapper for the Soundcloud SDK
MIT License
32 stars 8 forks source link

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

Open tryenc opened 8 years ago

tryenc commented 8 years ago

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();
        })
jakemmarsh commented 8 years ago

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?

tryenc commented 8 years ago

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.

jakemmarsh commented 8 years ago

Ah, okay. How are you retrieving your access_token?

tryenc commented 8 years ago

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);
    })

  };

}]);
akatrodiya commented 8 years ago

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!

TheMSB commented 8 years ago

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.