【问题标题】:NodeJS - How to use the twitch API with OAuth and code tokenNodeJS - 如何使用带有 OAuth 和代码令牌的 twitch API
【发布时间】:2019-11-02 12:18:36
【问题描述】:

嘿,今天我尝试使用 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}&amp;client_secret=${twitchClientSecret}&amp;code=${requestToken}&amp;grant_type=authorization_code&amp;redirect_uri=localhost:8080/twitch-oauth

我返回{status: 400, message: "Parameter redirect_uri does not match registered URI"}

有人可以帮助我吗?感谢您的回复

【问题讨论】:

    标签: javascript node.js oauth twitch twitch-api


    【解决方案1】:
    {status: 400, message: "Parameter redirect_uri does not match registered URI"}
    

    响应基本上是在告诉您:'Bad Request',因为您的请求中有参数redirect_uri

    localhost:8080/twitch-oauth 尚未注册为有效的重定向 URI

    您可能必须转到 OAuth 部分并将 localhost:8080/twitch-oauth 设置为有效的 redirect_uri,以便 Twitch 平台允许重定向并正确处理请求。

    来自 Twitch 开发文档:

    redirect_uri : URI :
    您注册的重定向 URI。这必须与之前注册的重定向 URI 完全匹配,Registration 步骤。您注册的重定向 URI。这一定是

    希望这能解决您的问题。 祝你好运!

    【讨论】:

    • 感谢您的回复!但我试图改变抽搐控制台上的值,但没有任何改变image
    猜你喜欢
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2017-11-18
    • 2011-02-18
    • 2012-01-29
    • 2020-07-26
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多