Getting 403 error while connecting to RapidAPI service
I’m working on integrating a vocabulary lookup API from RapidAPI into my Android application. However, I keep getting a 403 Forbidden error when making requests.
Error details:
I/OkHttpClient: Request failed for https://lexicon-service-v1.p.rapidapi.com/...
java.io.IOException: Unexpected code Response{protocol=http/1.1, code=403, message=Forbidden, url=https://lexicon-service-v1.p.rapidapi.com/definition/?word=example}
at com.example.myapp.WordProcessor$1.onResponse(WordProcessor.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.currentThread().getName());
My current implementation:
RapidApiConnect apiConnection = new RapidApiConnect("my-app-name", "unsure-about-project-key");
String searchTerm;
String apiHost = "lexicon-service-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_processor);
searchTerm = getIntent().getStringExtra("Search Term");
Log.d("DEBUG", searchTerm);
OkHttpClient httpClient = new OkHttpClient();
Request apiRequest = new Request.Builder()
.url("https://lexicon-service-v1.p.rapidapi.com/definition/?word=example")
.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 request, IOException e) {
Log.d("DEBUG", "request failed");
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
Log.d("DEBUG", "processing response");
try (ResponseBody body = response.body()) {
if (!response.isSuccessful()) {
Log.d("DEBUG", "bad response code");
throw new IOException("Unexpected code " + response);
}
Headers headers = response.headers();
Log.d("DEBUG", "checking headers");
for (int i = 0, size = headers.size(); i < size; i++) {
System.out.println(headers.name(i) + ": " + headers.value(i));
}
System.out.println(body.string());
}
}
});
}
I think the 403 error happens because I’m not sure about the project key parameter. I can’t locate this information on the RapidAPI dashboard. Is this configuration issue or am I missing something in my code? This is my first time working with external APIs so any guidance would be helpful.