Ad

Soundcloud Login With NodeJS And Liking Sounds Issue - 401 Unauthorized

I've been working on this for the past day and can't seem to figure it out. I am using this Passport-Soundcloud to implement a soundcloud authentication. However what I don't understand is how I can get, and pass an authentication token to a front-end button push to like a sound.

My front-end code looks like:

function allowLike(){
  $('.queueTrack').off('click').on('click', function(user){
    console.log('clicked');
      SC.put('/me/favorites/' + 21928809);
    consol.log('sound liked')

  });
};

Whenever I try to login through my app using the /login route, it works as expected and redirects me to my homepage. The problem is that I don't know how to get the oauth token from the passport-soundcloud so I can implement it into the front-end click event.

My routes followed the passport-soundcloud instructions and seem to work, but I can't figure out how to get the oauth token...

Any ideas? I'm totally lost on this.

Ad

Answer

So, I'm not familiar with the specific details of the soundcloud api. But if it follows the basic patterns of popular oauth apis. Then you'll want to do something like this.

  1. User arrives at your site without a cookie
  2. they authorize your app using oauth
  3. when the user is redirected back to your app, SoundCloud will give you an access key for this user. Store this value, in a database or a cache or in memory. But most importantly, you must create a cookie on that users browser. So that when they return you can lookup the access key again.
  4. When the user clicks like, lookup the accesskey on the backend and hit SoundCloud api with that token.

In the initial oauth flow....

function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({
        soundcloudId: profile.id,
        token: accessToken // <--- store the token!
    }, function (err, user) {
        return done(err, user);
    });
}

then when they click like

app.put('/me/favorites/12345' function(req, res) {
    var id = req.user.id; // <--- passport and connect session saved the user id for you
    // lookup the token in your database
    // use the token to hit the soundcloud api
})

I hope this make some kind of sense. This is completely untested pseudo code.

Ad
source: stackoverflow.com
Ad