简体   繁体   English

使用节点调度和重定向从 Spotify 获取令牌

[英]Getting token from Spotify using node-schedule and redirect

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.我想在服务器上安排我的几个服务(使用其他 spotify API)每天自动运行一次,但是它们需要访问令牌。 So my idea was to create a job to trigger the /login and redirect to /callback and get the token for further processing.所以我的想法是创建一个作业来触发/login并重定向到/callback并获取令牌以进行进一步处理。

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到目前为止,我尝试了 axios 但它无法处理重定向,而且我还没有弄清楚如何设置任何 http 服务以在工作中自动完成,使用node-schedule

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.您可以在授权代码流文档中阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM