Reputation: 21
I've made an app that has a search function that is set to search from a custom Google search I made (using their Custom Search service). This doesn't have a Java library, so I'm using Retrofit to request using the REST API (https://developers.google.com/custom-search/json-api/v1/using_rest), however I've run into a problem:
Retrofit is working fine, but the request always returns a 400 error with the following information: "domain": "usageLimits", "reason": "keyInvalid"
The API key I use in the request is the Android application key for my project in Google dev console. I assume this doesn't work however because Retrofit doesn't let Google know that I'm sending the request from my app?
What are the possible fixes for this situation?
Thanks.
EDIT: I've confirmed that my APK is signed with the correct keys, and I triplequadruplequintuple-everything checked the SHA1 used to generate the apikey. I've generated a new api key about 3 or 4 times to see if that fixes it.
I got it to work!
You don't make an Android API key (it does not work with this), you make a browser API key, in which you set the allowed referers to all (potentially dangerous).
You use that API key to request using Retrofit. I feel like this should be noted by Google: no doubt many would assume Android app? -> Android API key - but that is only the case when Google has made a library for the API.
Upvotes: 2
Views: 1609
Reputation: 20143
Sounds like you simply need to pass the key as a query parameter into your rest interface.
As an example, if you wanted to execute this GET command below
GET https://www.googleapis.com/customsearch/v1?key=INSERT_YOUR_API_KEY&cx=017576662512468239146:omuauf_lfve&q=lectures
Then you interface would be declared like so
public interface RestApi {
GET ("/customsearch/v1")
Response customSearch(@Query("key") String key, @Query("cx") String cx, @Query("q") String query);
}
Then you can execute the call like so
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://www.googleapis.com")
.build();
RestApi restApi = restAdapter.create(RestApi.class)
restApi.customSearch("INSERT_YOUR_API_KEY", "017576662512468239146:omuauf_lfve", "lectures");
Now don't forget to replace the INSERT_YOUR_API_KEY with your key that you've generated from the Google Dev Console.
Upvotes: 2