2014-12-07 145 views
1

我試圖找到幫助谷歌自定義搜索API的圖像。Android和谷歌自定義搜索API

每當我收到錯誤403. 我在做什麼不正確?

我在谷歌開發者控制檯註冊了項目,打開了自定義搜索API併爲Android應用程序創建了Key。

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

每次我得到的結果:

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

我有同樣的問題,但不能exacly。我的一個是「在您的API密鑰上配置了per-IP或per-Referer限制,並且請求不符合這些限制。如果來自此IP或引用者的請求應該是Google Developers Console,請使用Google Developers Console更新您的API密鑰配置允許的。「最類似的是,您尚未啓用自定義搜索API。你必須去console.developers.google.com,打開你的項目。轉到「API」並打開「自定義搜索API」開關。 – 2015-02-22 08:21:25

回答

3

雖然很晚的答案,但它也許對別人有幫助。 發生此問題是因爲您在提出請求時使用了錯誤的鍵。

而是「Android的重點」你要創建一個「瀏覽器主要」,並用它製作的request.You可以創建在谷歌API控制檯憑據部分瀏覽器的關鍵。

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