新的TwitchTV API调用

时间:2018-03-15 19:54:30

标签: javascript ajax

我是编程和挑战的初学者,我需要获得给定数量频道的状态(流媒体直播/离线)。

我写了一个ajax请求,但我仍然得到401作为回应。在我的代码下方和响应的屏幕截图。

我甚至将令牌请求设为async: false,以确保在触发API调用之前令牌已恢复

$("document").ready(function() {

var aoathToken = $.ajax({
    type: "POST",
    url: "https://id.twitch.tv/oauth2/token?client_id=MYCLIENTID&client_secret=MYSECRET&grant_type=client_credentials",
    error: function() {
        console.log("Check your request!!")
    },

    success: function(token) {
        console.log(token);
    }
})

$.ajax({
    type: "GET",
    url: "https://api.twitch.tv/helix/streams?user_id=freecodecamp",
    contentType: ('application/x-www-form-urlencoded; charset=UTF-8'),
    crossDomain: true,
    header: {
        "Access-Control-Allow-Origin": "*",
        "Client-ID": "MY CLIENT ID",
        "Authorization": "Bearer " + aoathToken,
    },
    data: {"user_login": "myUserID"},
    dataType: "json",
    success: function(json) {
        console.log(JSON.stringify(json, null, 2));
    },
    error: function(json) {
        window.alert(json.status + " " + json.message);
    },
})

API response

0 个答案:

没有答案
相关问题