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.