1

I am working on an app in Angular-Meteor with xinranxiao:spotify-web-api package,which is built off of spotify-node-api & xinranxiao:meteor-accounts-spotify package to login with spotify.

I can login successfully with spotify by creating a client Id and client Secret provided by Spotify Web Api and a redirect uri which is just http://localhost:3000/_oauth/spotify?close. I have a page where I list the users playlists and it shows the album artwork. Just below the album artwork I place spotify's frame player with the link to the above album/playlist but there's a 401 error. Need authentication.

I have logged in but I still need authentication ?

If i type Meteor.loginWithSpotify() in my chrome browser console, I get undefined and I don't see my access token? How Can I obtain my access token and refresh it ? I believe this is the cause for it showing I still need authentication and why if I type Meteor.loginWithSpotify() I get undefined.

In my server side Meteor code I have:

ServiceConfiguration.configurations.update(
  { "service": "spotify" },
  {
    $set: {
      "clientId": "000000000000000000000000000000",
      "secret": "000000000000000000000000000000"
    }
  },
  { upsert: true }
)

In my client side Meteor Methods I have a getUserPlaylists Method which does what it says and it works. Below it is a method checkTokenRefreshed which I am still not sure if it's refreshing the token?:

Meteor.methods({

  // Get a user's playlists
  getUserPlaylists: function() {
    //Spotify call
    var spotifyApi = new SpotifyWebApi()
    //response object
    var userplaylists = spotifyApi.getUserPlaylists(Meteor.user().profile.id,function(err,data){
      if(err){
        console.log("Retrieval error ", err)
      }
      else{
        console.log("Success, your playlist ", data.body)
      }
    })
    //Need to refresh token
    if(checkTokenRefreshed(userplaylists, spotifyApi)){
      userplaylists = spotifyApi.getUserPlaylists(Meteor.user().profile.id,function(err,data){
        if(err){
          console.log("Retrieval error ", err)
        }
        else{
          console.log("Success, your playlist ", data.body)
        }
      })//end response
    }//end checkTokenRefreshed

    return userplaylists

  }//end getUserPlaylists

})//end Meteor.methods

var checkTokenRefreshed = function(response, api) {
  if (response.error && response.error.statusCode === 401) {
    api.refreshAndUpdateAccessToken();
    return true
  } else {
    return false
  }
}

In my client side code I have:

var options = {
  showDialog: true, // Whether or not to force the user to approve the app again if they’ve already done so.
  requestPermissions: ['user-read-email','playlist-modify-private', 'user-library-read','user-follow-read', 'playlist-read-private','streaming'] // Spotify access scopes.
};

var scopes = ['user-read-email','playlist-modify-private', 'user-library-read','user-follow-read', 'playlist-read-private','streaming']
Accounts.ui.config({'requestPermissions':{'spotify':scopes}})

Meteor.loginWithSpotify(options, function(accessToken) {
  console.log('accessToken is ', accessToken)
});

The last method of the client side code should print my accessToken to the console but instead it prints accessToken is undefined


I should mention I can access a users playlist with node/meteor-spotify-web-api spotifyApi.getUserPlaylists() methods, but what good does that do if I can't post the link to it without getting a 401 error

The documentation for xinranxiao:meteor-spotify-api says 2) Get an oauth access_token, either through xinranxiao:accounts-spotify, or directly through this API (refer here. ) for how).

Felice
  • 571
  • 1
  • 7
  • 22
  • 1
    I think it would be best to try to get support in the [accounts-spotify github repo](https://github.com/xinranxiao/meteor-accounts-spotify). I have worked with the Spotify Web API and I would love to help but I don't know what the underlying requests look like. Is there any chance you can create a small example of the code failing and publish it somewhere so I can give it a try? – José M. Pérez Mar 02 '16 at 18:43
  • Hey Thanks @JoséM.Pérez . So i don't know which part of the code is failing, because I don't know how it's accessing the data. Here's a link to my [repo](https://github.com/felicedeNigris/MixtapeForum) I believe the error is in `MixtapeForum/client/spotifyclientlogin.js` or `MixtapeForum/server/SpotifyMethods.js`. In both these places I should interact with access token. If you prefer I can try to make a fiddle, but I am using MeteorJS so I don't think it will be the same. – Felice Mar 02 '16 at 19:50
  • A fiddle or meteorpad.com would be great, im dealing with the same problem! – Vin Banton Mar 02 '16 at 22:54

0 Answers0