嘿,今天我尝试使用 twitch api 通过此端点获取用户数据,
GET https://api.twitch.tv/helix/users
我想使用身份验证功能来跳过 api 限制。
我选择 ExpressJs 作为 Web 框架
这是我的 app.js 文件(入口点)
const express = require('express')
const axios = require('axios')
const app = express()
const twitchClientSecret = process.env.twitchAppToken || require('./creditentials.json').twitchAppToken
const twitchClientID = process.env.twitchClientId || require('./creditentials.json').twitchClientId
app.use(express.static(__dirname + '/public'));
app.get('/twitch-oauth', (req, res) => {
console.log("hey")
const requestToken = req.query.code
console.log("requesttoken:" + requestToken)
// if (req.query.code) {
axios({
method: 'post',
url: `https://id.twitch.tv/oauth2/token?client_id=${twitchClientID}&client_secret=${twitchClientSecret}&code=${requestToken}&grant_type=authorization_code&redirect_uri=localhost:8080/twitch-oauth`,
headers: {
accept: 'application/json'
}
}).then((response) => {
console.log(response)
console.log("response" + response)
const accessToken = response.data.access_token
res.cookie('twitch_id_token', accessToken)
res.redirect('/')
}).catch((error) => {
// Error
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
console.log(error.config);
});
// }
})
// Start the server on port 8080
app.listen(8080)
这是我的 index.html (使用 HTTP 提供的文件__dirname + '/public'
)
<a id="twitch-auth" href="https://id.twitch.tv/oauth2/authorize?client_id=6mrxy6lqs4m9svcim8ssr44ypzvk1c&redirect_uri=http://localhost:8080/twitch-oauth&response_type=code&scope=user:read:email&state=">
Login with Twitch
</a>
<script>
document.querySelector("#twitch-auth").href = document.querySelector("#twitch-auth").href + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
</script>
<script src="logic.js"></script>
最后是我的 logic.js 文件
const token = Cookies.get('twitch_id_token')
var cookie = document.cookie;
console.log(token);
if (token) {
fetch('https://api.twitch.tv/helix/users?id=44322889', {
method: 'GET',
headers: {
Accept: '*/*',
Authorization: 'Bearer ' + token,
}
})
// Parse the response as JSON
.then(res => res.json())
.then(res => {
console.log(res)
});
}
问题
当我单击“使用 Twitch 登录”按钮时,当 axios 发布此请求时出现 http 错误:https://id.twitch.tv/oauth2/token?client_id=${twitchClientID}&client_secret=${twitchClientSecret}&code=${requestToken}&grant_type=authorization_code&redirect_uri=localhost:8080/twitch-oauth
我回来了{status: 400, message: "Parameter redirect_uri does not match registered URI"}
有人能帮我吗 ?感谢您的回复