1
votes

I'm trying find images with help google custom search api.

Everytime i'm getting error 403. What i'm doing incorrect?

I registered project in google developer console, turned on Custom Search API and created Key for Android applications.

I have api-key and cx id

Code for getting search results:

String key = "AIzaSyBixxZ28popSAyP0YdlzvnWFECXktLQR4w";
String cx = "009034774129468977321:wl08kmocg3m";
String qry = "spring";// search key word

try {
    HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) throws IOException {
        }
    };

    JsonFactory jsonFactory = new JacksonFactory();
    HttpTransport httpTransport = new NetHttpTransport();

    Customsearch customsearch = new Customsearch.Builder(httpTransport, jsonFactory, httpRequestInitializer)
            .setApplicationName("CTTProject")
            .build();

    Customsearch.Cse.List list = customsearch.cse().list(qry);
    list.setKey(key);
    list.setCx(cx);
    Search results = list.execute();
    List<Result> items = results.getItems();

    for (Result item : items) {
        Log.d("Response", item.toString());
    }

} catch (IOException e) {
    e.printStackTrace();
}

everytime I'm getting result:

  886-895/gsihome.reyst.ctt W/System.err﹕ com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
  886-895/gsihome.reyst.ctt W/System.err﹕ {
  886-895/gsihome.reyst.ctt W/System.err﹕ "code" : 403,
  886-895/gsihome.reyst.ctt W/System.err﹕ "errors" : [ {
  886-895/gsihome.reyst.ctt W/System.err﹕ "domain" : "usageLimits",
  886-895/gsihome.reyst.ctt W/System.err﹕ "message" : "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration.",
  886-895/gsihome.reyst.ctt W/System.err﹕ "reason" : "accessNotConfigured",
  886-895/gsihome.reyst.ctt W/System.err﹕ "extendedHelp" : "https://console.developers.google.com"
  886-895/gsihome.reyst.ctt W/System.err﹕ } ],
  886-895/gsihome.reyst.ctt W/System.err﹕ "message" : "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration."
  886-895/gsihome.reyst.ctt W/System.err﹕ }
1
I have a same problem, but not exacly. Mine one is "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.". Most likey, you haven't enabled custom search API. You have to go to console.developers.google.com, open your project. Go to "APIs" and turn the "Custom Search API" switch on.Юрій Мазуревич

1 Answers

3
votes

Although a very late answer but it maybe helpful for others. This problem happens because you are using the wrong key in making the request.

Instead of "Android Key" you have to create a "Browser Key" and use it for making the request.You can create Browser Key from Credentials section in the Google API console.

Example

https://www.googleapis.com/customsearch/v1?q=a&key={BROWSER__KEY}&cx={SEARCH_ENGINE_KEY}