简体   繁体   English

如何存储 Spotify api 的访问令牌。 我正在使用passport.js

[英]How to store the access token for Spotify api. I am using passport.js

I am using passport.js to authenticate to Spotify api, I am logging in correctly, no problem with that.我正在使用 passport.js 向 Spotify api 进行身份验证,我登录正确,没问题。

But now I want to store the access token or display something in console that gives back some information about the user so I can use the access token to create playlist.但是现在我想存储访问令牌或在控制台中显示一些内容,以返回有关用户的一些信息,以便我可以使用访问令牌来创建播放列表。 How can I store the access token?如何存储访问令牌?

If I'm understanding you correctly, you want to store the Spotify API Access Token so it can be re-used across sessions.如果我对您的理解正确,您想存储 Spotify API 访问令牌,以便可以在会话中重复使用。

If I'm correct, you should be able to write the token to a file.如果我是正确的,您应该能够将令牌写入文件。

const fs = require('fs');
// passport.js stuff here
fs.writeFile('./.spotify-token', '< the access token >', (err) => {
    if (err) throw new Error('Failed to write ./.spotify-token: ' + err);
});

And it can be read like this:它可以这样读:

const token = fs.readFileSync('./.spotify-token'); // => String (the token) or throws an error

With this, you can feed the token back into Passport.有了这个,您可以将令牌反馈回 Passport。

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

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