thelinmichael/spotify-web-api-node

spotifyApi.setAccessToken is not a function

ritvik130 opened this issue · 0 comments

I am getting the above error while trying to set my own access_token:

const express = require('express');
const router = express.Router();
const path = require('path');
const spotify = require("../spotify/spotify")
const spotifyApi = require('spotify-web-api-node');

router.get('/displayName', async (req, res) => {
  try {
    // Get the access token from the cookie
    const access_token = req.cookies.access_token;  
    // Set the access token in the Spotify API client
    spotifyApi.setAccessToken(access_token);
    // Use Spotify API client to get user data
    const data = await spotifyApi.getMe();
    const displayName = data.body.display_name;
    // Display "Hello 'username'" on page
    const greeting = `<p>Hello ${displayName}!</p>`;
    res.send(greeting);
  } catch (error) {
    console.error(error);
    res.status(500).send('Internal Server Error');
  }
});
module.exports = router;

On my server logs, I get:

TypeError: spotifyApi.setAccessToken is not a function
According to the documentation I can use this function to set my own access token

I am successfully able to get access_token using:

async function getAccessToken(clientId,clientSecret) {
    let spotifyApiConfig = {
        url: 'https://accounts.spotify.com/api/token',
        method: 'POST',
        headers: {
          'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
      },
        params: {grant_type: 'client_credentials'}
      }
    const response = await axios(spotifyApiConfig);
    console.log(respone.data);
    access_token = response.data.access_token;
    return response.data.access_token
};

What am I missing here? Is there something I am doing wrong?