我正在做一些音乐网站投资组合与Spotify,NextJS 我想在这个网站上播放音乐,需要Spotify认证才能使用它. 当我点击登录按钮并获得405 HTTP状态时. 我查了一下postman ,也发现了同样的错误. 我搜索,它可能是错误的GET/POST问题,但我不能处理在哪里修复它.
应用程序/API/登录代码部分
export async function LoginHandler (req: NextApiRequest, res: NextApiResponse) {
const state = generateRandomString(16);
const CLIENT_ID = process.env.NEXT_PUBLIC_SPOTIFY_CLIENT_ID;
const scope =
"user-read-private user-read-email user-read-playback-state user-modify-playback-state streaming";
res.redirect(
"https://accounts.spotify.com/authorize?"+
qs.stringify({
response_type: "code",
client_id: CLIENT_ID,
scope: scope,
redirect_uri: `${REDIRECT_URL}/api/callback`,
state: state,
})
);
我使用useRouter(下一步/导航)来路由它.
<button
onClick={() => {
router.push("/api/login");
}}
>
这个是获取令牌代码
export const getAccessTokenData = () => {
return axios<AccessTokenData>({
method: "POST",
url: SPOTIFY_ACCESS_TOKEN_URL,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization:
"Basic " +
Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString("base64"),
},
data: {
grant_type: "client_credentials",
},
});
};
export const getAuthorizationTokenData = (code: string) => {
return axios<AuthorizationTokenData>({
method: 'post',
url: 'https://accounts.spotify.com/api/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic ' +
Buffer.from(
`${CLIENT_ID}:${CLIENT_SECRET}`
).toString('base64'),
},
data: {
code: code,
redirect_uri: REDIRECT_URL + '/api/callback',
grant_type: 'authorization_code',
},
});
}