Issue with API Integration
I’m working on integrating a vocabulary lookup service from RapidAPI into my Android app and running into authentication problems. When I make the API call, I keep getting a 403 Forbidden response.
public class WordLookup extends AppCompatActivity {
private String selectedWord;
private String apiHost = "word-service-api.p.rapidapi.com";
private String myApiKey = "my-actual-api-key-here";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_lookup);
selectedWord = getIntent().getStringExtra("word_to_search");
fetchWordData();
}
private void fetchWordData() {
OkHttpClient httpClient = new OkHttpClient();
Request apiRequest = new Request.Builder()
.url("https://word-service-api.p.rapidapi.com/definitions/?word=" + selectedWord)
.get()
.addHeader("X-RapidAPI-Host", apiHost)
.addHeader("X-RapidAPI-Key", myApiKey)
.build();
httpClient.newCall(apiRequest).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("API_ERROR", "Request failed", e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
Log.e("API_ERROR", "Server returned error: " + response.code());
return;
}
String responseData = response.body().string();
Log.d("API_SUCCESS", responseData);
}
});
}
}
The error suggests authentication issues but I’m not sure if I’m missing some configuration step. I have my API key from RapidAPI dashboard but wondering if there are other required parameters I’m not including. Has anyone successfully integrated similar APIs and can point me in the right direction?