Android 和 Google 自定义搜索 API

标签 android google-custom-search

我正在尝试使用 help google custom search api 查找图像。

每次我收到错误 403。 我做错了什么?

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

我有 api-key 和 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();
}

每次我得到结果:

  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﹕ }

最佳答案

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

您必须创建一个“浏览器 key ”,而不是“Android key ”,并使用它来发出请求。您可以从谷歌 API 控制台。

例子

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

关于Android 和 Google 自定义搜索 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27344111/

相关文章:

java - 如何禁用和启用 USB OTG 连接以打开和关闭硬件设备?

android - 在按钮上应用颜色过滤器

google-custom-search - 是否有 Google 自定义搜索休息 API 的工作示例?

css - Google CSE 自定义搜索 CSS

google-apps-script - 如何从 google apps 脚本中的 urlfetch 获取 Google 搜索结果

android - 需要我的 android 应用程序的设计建议

java - 为什么 AES java 解密返回额外的字符?

android - 在 Android Studio 中启动 AVD 时出错,错误表示 eglMakeCurrent 失败

javascript - 在自定义搜索中获取 JSON 图像

php - 如何将 PHP 客户端用于 Google 自定义搜索引擎