Привет, сегодня я попытался использовать twitch API для получения пользовательских данных с этой конечной точки
GET https://api.twitch.tv/helix/users
я хотел использовать функцию аутентификации, чтобы обойти ограничение API.
Я выбираю ExpressJs для веб-фреймворка
Вот мой файл 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», я получаю сообщение об ошибке http, когда axios POST этот запрос: 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"}
Кто-нибудь может мне помочь ? Спасибо за ваш ответ