thelinmichael / spotify-web-api-node

A Node.js wrapper for Spotify's Web API.
http://thelinmichael.github.io/spotify-web-api-node/
MIT License
3.1k stars 499 forks source link

"error":"invalid_client" in my Google Cloud app #276

Open marcz2007 opened 5 years ago

marcz2007 commented 5 years ago

`function playAngrySong(agent) { // Get the database collection 'dialogflow' and document 'agent' var randomNumber = Math.floor(Math.random() * (3 - 3 + 1)) + 3;

    return admin.database().ref(`${randomNumber}`).once('value').then((snapshot) => {
        const song = snapshot.child('song').val();
        const artist = snapshot.child('artist').val();

        agent.add(`I reccomend ${song} by ${artist}`);

        // Load the spotify web api node
        var SpotifyWebApi = require('spotify-web-api-node');

        // credentials for the spotify app endpoint
        var spotifyApi = new SpotifyWebApi({
            clientId: '744a1316f9374a27b7ec9bc7eabb0f92',
            clientSecret: '---secret---',
            redirectUri: 'https://www.getpostman.com/oauth2/callback'
        });

        spotifyApi.setAccessToken(
            'BQDjriAcf5p-0RbxnfXRQfIW1I2Gn_ogzzyl7vNAYh0XWhT8M-vRb2b3P8TUYOvzIDfxd783UkjJ5z-Lblu_vGWo_AG0yP1El3kpAKU21L3RMj3GDjWYaJh_R1sxoHnoc_V1FqGlQBJp9Gs9D0gfUIMq5AF5mFxrO_xauG867eBy_vgFDeFL5AXqOhEA4Q'
        );

        // Get Elvis' albums

        spotifyApi.getAudioAnalysisForTrack('4AKUOaCRcoKTFnVI9LtsrN').then(
            function(data) {
                console.log('Artist albums', data.body);
                var audioAnalysis = JSON.stringify(data.body);
                agent.add(`Here is the audio analysis for a song: ${audioAnalysis}`);
            },
            function(err) {
                console.error(err);
            }
        );
        agent.add(`Here is the audio analysis for a song: ${audioAnalysis}`);
    });
}

`

In the above code I am getting a {"error":"invalid_client"} error, any ideas as to why this is? I have used the client id and the secret id as per my dashboard on spotify. The redirect uri is also the same in both. Why is it saying I am an invalid client?

DanielFroehlich commented 5 years ago

Check your access token. Is it really hard coded as shown in above code? Access token is usually valid only for one hour. I got these "Invalid Client" errors when the token expired. You have to refresh it using the refresh token you get on the auth callback. Checkout https://developer.spotify.com/documentation/general/guides/authorization-guide/ and the examples around spotifyApi.refreshAccessToken().

On Sun, Jul 28, 2019 at 5:05 PM marcz2007 notifications@github.com wrote:

`function playAngrySong(agent) { // Get the database collection 'dialogflow' and document 'agent' var randomNumber = Math.floor(Math.random() * (3 - 3 + 1)) + 3;

return admin.database().ref(${randomNumber}).once('value').then((snapshot) => { const song = snapshot.child('song').val(); const artist = snapshot.child('artist').val();

  agent.add(`I reccomend ${song} by ${artist}`);

  // Load the spotify web api node
  var SpotifyWebApi = require('spotify-web-api-node');

  // credentials for the spotify app endpoint
  var spotifyApi = new SpotifyWebApi({
      clientId: '744a1316f9374a27b7ec9bc7eabb0f92',
      clientSecret: '---secret---',
      redirectUri: 'https://www.getpostman.com/oauth2/callback'
  });

  spotifyApi.setAccessToken(
      'BQDjriAcf5p-0RbxnfXRQfIW1I2Gn_ogzzyl7vNAYh0XWhT8M-vRb2b3P8TUYOvzIDfxd783UkjJ5z-Lblu_vGWo_AG0yP1El3kpAKU21L3RMj3GDjWYaJh_R1sxoHnoc_V1FqGlQBJp9Gs9D0gfUIMq5AF5mFxrO_xauG867eBy_vgFDeFL5AXqOhEA4Q'
  );

  // Get Elvis' albums

  spotifyApi.getAudioAnalysisForTrack('4AKUOaCRcoKTFnVI9LtsrN').then(
      function(data) {
          console.log('Artist albums', data.body);
          var audioAnalysis = JSON.stringify(data.body);
          agent.add(`Here is the audio analysis for a song: ${audioAnalysis}`);
      },
      function(err) {
          console.error(err);
      }
  );
  agent.add(`Here is the audio analysis for a song: ${audioAnalysis}`);

}); }

`

In the above code I am getting a {"error":"invalid_client"} error, any ideas as to why this is? I have used the client id and the secret id as per my dashboard on spotify. The redirect uri is also the same in both. Why is it saying I am an invalid client?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/thelinmichael/spotify-web-api-node/issues/276?email_source=notifications&email_token=AENMQW3G2PNXJPOYR7Y2JPTQBWYUVA5CNFSM4IHMWTLKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HB45AOQ, or mute the thread https://github.com/notifications/unsubscribe-auth/AENMQWZUXDF7VVHESN7RZBLQBWYUVANCNFSM4IHMWTLA .