我一直在为Spotify开发OAuth2流,遵循FireBase团队针对Instagram的类似教程。
HERE
我可以提交我的凭证并在URL中返回用户代码和状态,但是当我运行提交代码以返回身份验证令牌的方法时,我在FireBase函数中打印到控制台的身份验证令牌将返回:
Auth Token Error Not Found
. 这是我的工作流程:
这里是
Spotify docs
首先,我有一个配置spotifyoauth的函数:
function spotifyOAuth2Client() {
// Spotify OAuth 2 setup
const credentials = {
client: {
id: functions.config().spotify.clientid,
secret: functions.config().spotify.clientsecret,
},
auth: {
tokenHost: 'https://accounts.spotify.com',
authorizePath: '/authorize'
},
};
return require('simple-oauth2').create(credentials);
}
我在这个firebase函数中使用这个函数
https://us-central1-<my project string>.cloudfunctions.net/redirect
:
exports.redirect = functions.https.onRequest((req, res) => {
const oauth2 = spotifyOAuth2Client();
cookieParser()(req, res, () => {
const state = req.cookies.state || crypto.randomBytes(20).toString('hex');
console.log('Setting verification state:', state);
res.cookie('state', state.toString(), {
maxAge: 3600000,
secure: true,
httpOnly: true,
});
const redirectUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: OAUTH_REDIRECT_URI,
//scope: OAUTH_SCOPES,
state: state,
});
console.log('Redirecting to:', redirectUri);
res.redirect(redirectUri);
});
});
上面的代码返回一个带有适当参数的URL字符串,下面的代码块是我的代码断开的地方,我有另一个云函数,在从
res.redirect(redirectUri)
上面。当我试图运行
getToken()
方法,它似乎没有返回任何内容,因为我点击了catch块?这就是我观察的地方
找不到身份验证令牌错误
.
const oauth2 = spotifyOAuth2Client();
try {
return cookieParser()(req, res, async () => {
console.log('Received verification state:', req.cookies.state);
console.log('Received state:', req.query.state);
if (!req.cookies.state) {
throw new Error('State cookie not set or expired. Maybe you took too long to authorize. Please try again.');
} else if (req.cookies.state !== req.query.state) {
throw new Error('State validation failed');
}
console.log('Received auth code:', req.query.code);
console.log(OAUTH_REDIRECT_URI);
// Get the access token object (the authorization code is given from the previous step).
const tokenConfig = {
code: req.query.code,
redirect_uri: 'http://localhost:8100/popup'
};
// Save the access token
try {
const result = await oauth2.authorizationCode.getToken(tokenConfig)
const accessToken = oauth2.accessToken.create(result);
console.log('inside try');
console.log(result);
console.log(accessToken);
} catch (error) {
console.log('Access Token Error', error.message);
}
我已经在配置中仔细检查了我的spotify客户机/机密凭据,这个OAuth2流出了什么问题?