【发布时间】:2014-12-07 15:19:52
【问题描述】:
我正在尝试借助 google 自定义搜索 api 查找图像。
每次我收到错误 403。 我做错了什么?
我在谷歌开发者控制台中注册了项目,打开了自定义搜索 API 并为 Android 应用程序创建了密钥。
我有 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﹕ }
【问题讨论】:
-
我也有同样的问题,但不完全是。我的一个是“在您的 API 密钥上配置了 per-IP 或 per-Referer 限制,并且请求与这些限制不匹配。如果来自此 IP 或引用者的请求应该是,请使用 Google Developers Console 更新您的 API 密钥配置允许。”。最有可能的是,您尚未启用自定义搜索 API。你必须去console.developers.google.com,打开你的项目。转到“API”并打开“自定义搜索 API”开关。
标签: android google-custom-search