我一直在遵循Firebase团队针对Instagram HERE
进行的类似教程工作,从而致力于Spotify的oauth2流程我能够提交我的凭据,并在url中返回用户代码和状态,但是当我运行方法以提交代码以返回auth令牌时,我打印到Firebase函数中控制台的auth令牌将返回: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);
}
我在使用https://us-central1-<my project string>.cloudfunctions.net/redirect
调用的Firebase函数中使用该函数:
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)
重定向后运行的cloud函数。当我尝试运行getToken()
方法时,它似乎未返回任何内容,因为我撞到了catch块?我在这里观察Auth Token Error Not Found
。
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流出了什么问题?
答案 0 :(得分:0)
解决了我的问题,我没有使用正确的端点:
const credentials = {
client: {
id: functions.config().spotify.clientid,
secret: functions.config().spotify.clientsecret,
},
auth: {
tokenHost: 'https://accounts.spotify.com',
authorizePath: '/authorize',
tokenPath: '/api/token'
},
};