0

I am currently trying to learn MVVM by creating a news app and using the rest API at https://newsapi.org. I am calling the API using retrofit2, but I keep getting a error. I debugged the app by placing a breakpoint at the position where I am suppose to receive the response and the problem is that when I try to make a call to the API in android studio using the API key I always get a 401 error, but when I make the same call in a browser I am successful. Why is this?

Here is my code.

API Interface

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ApiInterface {
@GET("top-headlines")
Call<ArrayList<NewsArticles>> getNewsList(@Query("country")String country, @Query("apiKey") String 
apiKey);
}

The Repository where I make the call.

public class Repository {

public Repository() {
}

static final String BASE_URL = "https://newsapi.org/v2/";

MutableLiveData<ArrayList<NewsArticles>> newsArticleList = new MutableLiveData<>();

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

ApiInterface apiInterface = retrofit.create(ApiInterface.class);

Call<ArrayList<NewsArticles>> call = apiInterface.getNewsList("us","api");

public MutableLiveData<ArrayList<NewsArticles>> getCall() {
    call.enqueue(new Callback<ArrayList<NewsArticles>>() {
        @Override
        public void onResponse(Call<ArrayList<NewsArticles>> call, Response<ArrayList<NewsArticles>> response) {
            if (response.isSuccessful()) {
                newsArticleList.setValue(response.body());
            }
        }

        @Override
        public void onFailure(Call<ArrayList<NewsArticles>> call, Throwable t) {
          t.printStackTrace();
        }
    });
    return newsArticleList;
}


}
Daniel Edwards
  • 179
  • 1
  • 8

2 Answers2

0

Maybe there is an issue with the http headers you provide on both cases.

Try to debug by sniffing the http protocol data, and compare the two. See what you are missing...

javadev
  • 688
  • 2
  • 6
  • 17
0

Explicitly specifying your headers in the API Interface should work.

public interface ApiInterface {
   @Headers(
        value = [
            "Accept: application/json",
            "Content-type:application/json"]
    )
}
  • Thanks for your reply, tried this but it still does not work. This is what the debugger shows me: Response{protocol=h2, code=401, message=, url=https://newsapi.org/v2/top-headlines?country=us} – Daniel Edwards May 27 '21 at 16:21
  • Did you run it on localhost? `{Requests from the browser are not allowed on the Developer plan, except from localhost."}` – EjectorAirTime May 27 '21 at 18:43
  • Thanks man, I didn't know that you couldn't make a call outside of the localhost before. Sorry if it feels like I wasted your time, but this is my first time really using an api so I didn't really know about localhost before you bought it up. I think I will use another api instead then. Thanks. – Daniel Edwards May 28 '21 at 03:19
  • You can try this api: https://open-api.xyz/placeholder/blogs – EjectorAirTime May 28 '21 at 13:17