RAWG Video Games Database

KOSTENLOS
Durch accujazz | Aktualisiert vor 3 Monaten | Gaming
Popularität

9.8 / 10

Latenz

288ms

Service Level

100%

Zurück zu allen Diskussionen

401 Unauthorized Access

avatar
jpatterson933
vor 3 Monaten

For some reason the small application I was working on a few months ago as practice is no longer working. Nothing has changed on my end, I am curious why I am no longer able to query data and why I am now getting this 401 error. I see this is a common discussion question on here. Does anyone have any type of answers for this? Or should I just abandon this API?

------------------fetch request----------------------

function getGameAPI(searchTerm) {
var baseUrl = “https://rawg-video-games-database.p.rapidapi.com/games” + searchTerm;
return fetch(baseUrl, {
“method”: “GET”,
“headers”: {
“x-rapidapi-key”: “***********************************************”,
“x-rapidapi-host”: “rawg-video-games-database.p.rapidapi.com
}
});
}

const mario = “/mario”

getGameAPI(mario)
.then(function(response) {
console.log(response)
return response.json()
});

------------------ERROR IN CONSOLE---------------------

GET https://rawg-video-games-database.p.rapidapi.com/games/mario 401 (Unauthorized)
Response {type: “cors”, url: “https://rawg-video-games-database.p.rapidapi.com/games/mario”, redirected: false, status: 401, ok: false, …}

Z
ZeuS2017 commented vor 3 Monaten

You can go to https://rawg.io/apidocs and register for api key and replace key used in tut with this one and it’ll work

avatar
jpatterson933 commented vor 3 Monaten

Thanks! I gave that a try and for some reason it now tells me that I am not subscribed to the api 😦

I have this live on github. A branched repo if youd like to look at it. I usually give a crack at it a few times a week but have been utterly stumped.

Github Repo Link

I think maybe my fetch request might be wrong? I’ll post the code below.

//grabs the game information that is searched for
function getGameAPI(searchTerm) {
//joins are platforms selection
var platformsJoined = platforms.join(",");
var baseUrl = “https://rawg-video-games-database.p.rapidapi.com/games”;
var query = baseUrl + “?search=” + searchTerm + “&page_size=100&search_exact=true&platforms=” + platformsJoined;
// var query = baseUrl
//we now limit search terms to 5
console.log(query)
return fetch(query, {
“method”: “GET”,
“headers”: {
“x-rapidapi-host”: “rawg-video-games-database.p.rapidapi.com”,
“x-rapidapi-key”: “**********************************”
}
})
}

$("#find-game").on(“click”, function(event) {
event.preventDefault();
var game = $("#search-game").val().split(" “).join(”-");
getGameAPI(game)
console.log(getGameAPI())
.then(function(response) {
console.log(response, “first response”)
return response.json()
})
.then(function(data) {
console.log(data)
if(data.redirect) {
console.log(‘first condition triggered’)
getGameAPI(data.slug)
.then(function(response) {
return response.json();
})
.then(function(data1) {
console.log(data1);
});
} else {
console.log(‘else condition triggered’)
console.log(data)

        var gameNameStr = ' ';
        var playTimeStr = ' ';
        var gameImageStr = ' ';

        for (var i = 0; i < data.results.length; i++) {

            if (data.results[i].playtime > 0) {

                //puts game names into string and stores into local storage
                gameNameStr += data.results[i].name + ", ";
                localStorage.setItem("gameNameStr", gameNameStr);
                
                //puts game play times into a string and stores it into local storage
                playTimeStr += data.results[i].playtime + ", ";
                localStorage.setItem("gameTimeStr", playTimeStr);

                //puts our game images into a string
                gameImageStr += data.results[i].background_image + ", ";
                localStorage.setItem("gameImageStr", gameImageStr);

                //console log our search results
                console.log(data.results[i].name);
                console.log(data.results[i].playtime);
                console.log(data.results[i].released);
                console.log(data.results[i].background_image)

                //this reloads the page
                location.reload();
            }
        }
    }
})

})

I could use the help anyone has out there!

Also, the last comment on here as literally been posted multiple times. However, if you go to their api documenation page, it literally explains nothing about how to use the actual api. Of course, maybe I am missing something?

Nehmen Sie an der Diskussion teil - fügen Sie unten einen Kommentar hinzu:

Anmelden / Registrieren, um neue Kommentare zu veröffentlichen
Bewertung: 4.4 - Stimmen: 24