Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android和Google自定义搜索API_Android_Google Custom Search - Fatal编程技术网

Android和Google自定义搜索API

Android和Google自定义搜索API,android,google-custom-search,Android,Google Custom Search,我正在用谷歌自定义搜索api帮助查找图像 每次我都会收到错误403。 我做的不对吗 我在谷歌开发者控制台注册了这个项目,打开了自定义搜索API,并为Android应用程序创建了密钥 我有api密钥和cx id 获取搜索结果的代码: String key = "AIzaSyBixxZ28popSAyP0YdlzvnWFECXktLQR4w"; String cx = "009034774129468977321:wl08kmocg3m"; String qry = "spring";// sear

我正在用谷歌自定义搜索api帮助查找图像

每次我都会收到错误403。 我做的不对吗

我在谷歌开发者控制台注册了这个项目,打开了自定义搜索API,并为Android应用程序创建了密钥

我有api密钥和cx id

获取搜索结果的代码:

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();
}

虽然回答得很晚,但可能对其他人有帮助。 发生此问题的原因是您在发出请求时使用了错误的密钥

您必须创建一个“浏览器密钥”,并将其用于发出请求,而不是“Android密钥”。您可以从Google API控制台的凭据部分创建浏览器密钥

范例


{BROWSER\uuuu KEY}&cx={SEARCH\u ENGINE\u KEY}

我也有同样的问题,但不是很严重。我的第一个问题是“您的API密钥上配置了每IP或每引用者限制,请求与这些限制不匹配。如果允许来自此IP或引用者的请求,请使用Google开发者控制台更新您的API密钥配置。”。最有可能的是,您尚未启用自定义搜索API。您必须转到console.developers.google.com,打开您的项目。转到“API”并打开“自定义搜索API”开关。
  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﹕ }