3

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

1 Answers1

0
{status: 400, message: "Parameter redirect_uri does not match registered URI"}

The response is basically telling you : 'Bad Request' because the parameter redirect_uri in your request.

localhost:8080/twitch-oauth has not been registered as a valid redirection URI.

You probably have to go to the OAuth section and set localhost:8080/twitch-oauth as a valid redirect_uri, so the Twitch platform will allow the redirection and treat the request correctly.

From the Twitch Dev Documentation :

redirect_uri : URI :
Your registered redirect URI. This must exactly match the redirect URI registered in the prior, Registration step.Your registered redirect URI. This must exactly

Hope this clears your problem. Good luck!

EMX
  • 6,066
  • 1
  • 25
  • 32
  • Thanks for your reply! but i tried to change the values on the twitch console but nothing change [image](https://i.imgur.com/sSJcyZV.png) – Mattèo Gauthier Jun 19 '19 at 20:35