Search code examples
javaretrofitapi-key

Retrofit, IGDB Api - How to correctly pass Api Key?


I'm trying to make an query on the internet game database (IGDB) Api using retrofit. In the past, when I used other Api, such as the movie database (TMDB), the api-keys were always passed in as a query, so the interface would look something like this.

@GET("/3/movie/popular")
Call<MovieResults> getPopularMovie(
        @Query("api_key") String apiKey,
        @Query("language") String language,
        @Query("page") int page
);

But with IGDB, I think they want me to pass in the api-key as a Header (I'm not 100% sure, please correct me if I'm wrong). I've never passed an api-key through a header before, so with some guesswork, following is what my interface looks like.

@Headers({
        "Accept: application/json",
        "user-key: b5bd50####################8b2"
})
@GET("/games")
Call<List<Game>> getGame();

Problem is, when I make the call, although the call ends up in onResponse, the response.body is always null.

So, I ran the code in de-bug mode, and i found this message:

response = Response{protocol=http/1.1, code=401, message=Unauthorized, url=http://v2000.igdb.com/games/}

I don't know what this means, but I am guessing that it isn't recognizing an authorised api-key?

Another interesting anomaly is that, the base url I pass in is:

"https://api-2445582011268.apicast.io/"

I don't know how that ended up being:

http://v2000.igdb.com/

If anyone have any experience with IGDB, please give me some help, will really appreciate it.


Solution

  • I have used this api with JAVA and i can confirm that you should send the API key in the header, so what you have done looks to be correct.

    The base url you are getting is also wrong, try using https://api-endpoint.igdb.com/, it is the recommended url.

    I can also recommend you to try the api using Postman, it is an excellent tool for testing.

    If you are using java you might want to try their java wrapper