Getting 403 Forbidden Error When Integrating RapidAPI with Android App

I’m working on integrating a dictionary API from RapidAPI into my Android project but keep running into issues.

When I try to make requests to the API endpoint, I get a 403 Forbidden error. I think this might be because I’m not setting up the authentication correctly. I’m confused about where to find the project key on RapidAPI’s dashboard.

Here’s the error I’m getting:

I/OkHttpClient: Request failed for https://wordbook-api-v1.p.rapidapi.com/...
    java.io.IOException: Unexpected code Response{protocol=http/1.1, code=403, message=Forbidden, url=https://wordbook-api-v1.p.rapidapi.com/definition/?word=book}
        at com.example.myapp.WordLookup$1.onResponse(WordLookup.java:62)
        at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
        at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:760)

And here’s my code:

RapidApiConnect apiClient = new RapidApiConnect("my app name", "where do I get this key?");
String searchTerm;

String apiHost = "wordbook-api-v1.p.rapidapi.com";
String MY_API_KEY = "my actual api key here";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_word_lookup);

    searchTerm = getIntent().getStringExtra("Search Term");
    Log.d("DEBUG", searchTerm);
    OkHttpClient httpClient = new OkHttpClient();

    Request apiRequest = new Request.Builder()
            .url("https://wordbook-api-v1.p.rapidapi.com/definition/?word=book")
            .get()
            .addHeader("x-rapidapi-host", apiHost)
            .addHeader("x-rapidapi-key", MY_API_KEY)
            .build();

    httpClient.newCall(apiRequest).enqueue(new Callback() {
        @Override
        public void onFailure(Request req, IOException error) {
            Log.d("DEBUG", "request failed");
            error.printStackTrace();
        }

        @Override
        public void onResponse(Response resp) throws IOException {
            Log.d("DEBUG", "got response");

            try (ResponseBody body = resp.body()) {
                if (!resp.isSuccessful()) {
                    Log.d("DEBUG", "bad response code");
                    throw new IOException("Bad response " + resp);
                }

                Headers headers = resp.headers();
                Log.d("DEBUG", "checking headers");
                for (int i = 0, count = headers.size(); i < count; i++) {
                    System.out.println(headers.name(i) + ": " + headers.value(i));
                }

                System.out.println(body.string());
            }
        }
    });
}

I’m new to working with APIs so I’m not sure if the problem is with my code or if I’m missing some configuration step. Any help would be great since this is for my graduation project.

I experienced a similar issue when integrating APIs. The 403 Forbidden error usually indicates that your API key is either incorrect or not being properly recognized. Ensure that you retrieve the API key from the correct section in your RapidAPI dashboard, specifically within the API you are using, under ‘Code Snippets’.

Your current implementation of using both RapidApiConnect and OkHttpClient can lead to confusion; it’s advisable to stick to one approach. If you favor OkHttp, consider removing the RapidApiConnect instantiation. Additionally, verify that there are no leading/trailing spaces or formatting issues in your API key.

Lastly, confirm that you are subscribed to the desired API and that your current plan is sufficient for access as a basic free plan may not always grant pass-through rights.

check your rapidapi dashboard - your free trial might’ve expired, which would cause the 403 error. also verify you’re not hitting rate limits. try logging the actual api key value to make sure it’s reading correctly (just remove that before going live). your code looks good otherwise.

Check your header names for RapidAPI auth. Your error suggests the API isn’t recognizing your credentials. RapidAPI needs specific header formatting - use ‘X-RapidAPI-Key’ and ‘X-RapidAPI-Host’ with capital letters, not lowercase. I ran into the same auth issue last year and it was a case sensitivity thing with headers. Also grab your key from the endpoint testing section, not account settings - they’re different values. Still getting 403s after fixing headers? Test the request in RapidAPI’s built-in tool first to make sure your subscription’s active and you can actually access the endpoint.