Hey today i tried to use the twitch api to fetch user data with this endpoint
GET https://api.twitch.tv/helix/users
i wanted to use the authentification feature to skip the api-limit.
I choose ExpressJs for the web framework
Here is my app.js file (entry point)
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)
Here is my index.html (file served with 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>
And finally my logic.js file
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)
});
}
The problem
When i click the button "Login with Twitch" i get an http error when axios POST this request : https://id.twitch.tv/oauth2/token?client_id=${twitchClientID}&client_secret=${twitchClientSecret}&code=${requestToken}&grant_type=authorization_code&redirect_uri=localhost:8080/twitch-oauth
I returns {status: 400, message: "Parameter redirect_uri does not match registered URI"}
Can someone help me ? Thanks for your reply