Getting token from Spotify using node-schedule and redirect

Issue

Logging in and getting access token on client

const Login = () => {
  return (
    <>
     <button>
       <a href=http://localhost:3001/login`}>Login</a>
     </button>
    </>
  )
}

next, redirecting to server using the following code

const router = express.Router()

router.get('/login', async (req, res) => {
  res.redirect(loginUrl)
})
loginUrl = `https://accounts.spotify.com/authorize?client_id=${process.env.CLIENT_ID}&response_type=code&redirect_uri=http://localhost:3001/callback`
router.get('/callback', async (req, res) => {
  const code = req.query.code || null

// rest of the code where tokens are handled
}

I want to schedule couple of my services(using other spotify APIs) on server to run automatically once per day, however they require access token. So my idea was to create a job to trigger the /login and redirect to /callback and get the token for further processing.

How can I achieve this by calling it from the server? So far I tried axios but it can’t handle the redirection and I haven’t figure out how to setup any http service to do it automatically within the job yet, using node-schedule

Solution

To accomplish what you want, you need to make something that can request an access token and a refresh token. When the access token is expired, the server can request a new access token and a new refresh token with the refresh token.
You can read more about it in the Authorization Code Flow documentation.

Answered By – Ximzend

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published