gpt4 book ai didi

Android 和 Google 自定义搜索 API

转载 作者:行者123 更新时间:2023-11-29 17:44:51 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com