I’m trying to integrate the Twinword dictionary API into my Android Studio project, but I’m encountering a 403 Forbidden error. Here’s the code I’m using:
RapidApiConnector connector = new RapidApiConnector("Insert your project name here", "Where do I locate the project key?");
String resultData;
String apiHost = "twinword-twinword-bundle-v1.p.rapidapi.com";
String API_KEY = "your_api_key_here";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultData = getIntent().getStringExtra("ObjectName");
Log.d("DEBUG_LOG", resultData);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://twinword-twinword-bundle-v1.p.rapidapi.com/word_example/?entry=mask")
.get()
.addHeader("x-rapidapi-host", apiHost)
.addHeader("x-rapidapi-key", API_KEY)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException error) {
Log.d("DEBUG_LOG", "Request failed");
error.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
Log.d("DEBUG_LOG", "Received response");
try (ResponseBody body = response.body()) {
if (!response.isSuccessful()) {
Log.d("DEBUG_LOG", "Unexpected response code");
throw new IOException("Unexpected code: " + response);
}
Headers responseHeaders = response.headers();
Log.d("DEBUG_LOG", "Response headers");
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(body.string());
}
}
});
}
I think the error might be due to not having the correct Project Key because I’m unable to find it on the RapidAPI site. Additionally, I’m uncertain if my code setup has any issues. This is my first experience with API integrations, and I’d appreciate any help I can get.