无效的API密钥或未经批准的应用程序,从trakt-tv获取数据

时间:2019-05-29 14:28:14

标签: java android json retrofit2

我遇到了以下问题:我需要从trakt-tv获得流行电影的列表。为此,我创建了以下类:

public class MoviesListDownload extends AsyncTask<Void, Void, ArrayList<Movie>> {

    @Override
    protected ArrayList<Movie> doInBackground(Void... voids) {
        ArrayList<Movie> moviesList = new ArrayList<>();

        String baseUrl = "https://api.trakt.tv/";

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create());

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS);

        httpClient.addInterceptor(chain -> {
            Request original = chain.request();

            Request.Builder requestBuilder = original
                    .newBuilder()
                    .header("Content-Type", "application/json")
                    .header("trakt-api-version", "2")
                    .header("trakt-api-key", [auth_key]);
Request request = requestBuilder.build();
            return chain.proceed(request);
        });

        Retrofit retrofit = builder.build();

        APIService apiService = retrofit.create(APIService.class);
        Call<List<Movie>> moviesListCall = apiService.getMovies();

        try {
            Response<List<Movie>> result = moviesListCall.execute();
            Log.i("ResCode",result.code()+"");
            if (result.code() == 200) {
                moviesList.addAll(Objects.requireNonNull(result.body()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return moviesList;
    }

}

这是我需要获取的JSON:

[
    {
        "watchers": 119,
        "movie": {
            "title": "Captain Marvel",
            "year": 2019,
            "ids": {
                "trakt": 193963,
                "slug": "captain-marvel-2019",
                "imdb": "tt4154664",
                "tmdb": 299537
            }
        }
    },
    {
        "watchers": 26,
        "movie": {
            "title": "Avengers: Endgame",
            "year": 2019,
            "ids": {
                "trakt": 191798,
                "slug": "avengers-endgame-2019",
                "imdb": "tt4154796",
                "tmdb": 299534
            }
        }
    },
    {
        "watchers": 12,
        "movie": {
            "title": "Aquaman",
            "year": 2018,
            "ids": {
                "trakt": 193968,
                "slug": "aquaman-2018",
                "imdb": "tt1477834",
                "tmdb": 297802
            }
        }
    },
    {
        "watchers": 12,
        "movie": {
            "title": "Shazam!",
            "year": 2019,
            "ids": {
                "trakt": 184992,
                "slug": "shazam-2019",
                "imdb": "tt0448115",
                "tmdb": 287947
            }
        }
    },
    {
        "watchers": 10,
        "movie": {
            "title": "Aladdin",
            "year": 2019,
            "ids": {
                "trakt": 276911,
                "slug": "aladdin-2019",
                "imdb": "tt6139732",
                "tmdb": 420817
            }
        }
    },
    {
        "watchers": 9,
        "movie": {
            "title": "Avengers: Infinity War",
            "year": 2018,
            "ids": {
                "trakt": 191797,
                "slug": "avengers-infinity-war-2018",
                "imdb": "tt4154756",
                "tmdb": 299536
            }
        }
    },
    {
        "watchers": 9,
        "movie": {
            "title": "The Curse of La Llorona",
            "year": 2019,
            "ids": {
                "trakt": 327751,
                "slug": "the-curse-of-la-llorona-2019",
                "imdb": "tt4913966",
                "tmdb": 480414
            }
        }
    },
    {
        "watchers": 8,
        "movie": {
            "title": "Mary Poppins Returns",
            "year": 2018,
            "ids": {
                "trakt": 243783,
                "slug": "mary-poppins-returns-2018",
                "imdb": "tt5028340",
                "tmdb": 400650
            }
        }
    },
    {
        "watchers": 8,
        "movie": {
            "title": "Us",
            "year": 2019,
            "ids": {
                "trakt": 305845,
                "slug": "us-2019",
                "imdb": "tt6857112",
                "tmdb": 458723
            }
        }
    },
    {
        "watchers": 8,
        "movie": {
            "title": "The Grinch",
            "year": 2018,
            "ids": {
                "trakt": 235711,
                "slug": "the-grinch-2018",
                "imdb": "tt2709692",
                "tmdb": 360920
            }
        }
    }
]

这是我用来获取JSON的方法:

@GET ("movies/trending")
Call<List<Movie>> getMovies();

Movie类仅包含字段title,该字段具有注释SerializedName ("title")

当然,我知道Retrofit应该以另一种方式构建,作为单例对象,但是在这种情况下没有必要。当我运行这段代码时,我得到response.code() = 403的含义,因为它是蜂房403 Forbidden - invalid API key or unapproved app。但是我不明白,为什么会这样,就像我打开个人资料时写的那样,它批准的我的应用程序和密钥是有效的,并且与我使用的相同。那怎么了也许其他地方有错误?

0 个答案:

没有答案