2015-05-29 135 views
2

我試圖構建Android應用程序,可以從Google Geolocation Api中獲取用戶的位置。我的配置和設置結算帳戶Android Google Google Geolocation API返回403錯誤

我的設置是

  1. 增加配額至100 /請求/用戶
  2. 結算帳戶設置
  3. 允許的IP:使用異步任務的任何

IM類

// 1. create HttpClient 
 
       HttpClient httpclient = new DefaultHttpClient(); 
 

 
       // 2. make POST request to the given URL 
 
       HttpPost httpPost = new HttpPost("https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY"); 
 

 
       String json = ""; 
 

 
       // 3. build jsonObject 
 
       JSONObject jsonObject = new JSONObject(); 
 
       jsonObject.accumulate("homeMobileCountryCode", 310); 
 
       jsonObject.accumulate("homeMobileNetworkCode", 410); 
 
       jsonObject.accumulate("radioType", "gsm"); 
 
       jsonObject.accumulate("carrier", "vodafone"); 
 

 
       JSONArray cellTowersArray = new JSONArray();; 
 
       JSONObject cellTowerObject = new JSONObject();; 
 
       cellTowerObject.accumulate("cellId", 42); 
 
       cellTowerObject.accumulate("locationAreaCode", 415); 
 
       cellTowerObject.accumulate("mobileCountryCode", 310); 
 
       cellTowerObject.accumulate("mobileNetworkCode", 410); 
 
       cellTowerObject.accumulate("age", 0); 
 
       cellTowerObject.accumulate("signalStrength", -60); 
 
       cellTowerObject.accumulate("timingAdvance", 15); 
 
       cellTowersArray.put(cellTowerObject); 
 
       jsonObject.accumulate("cellTowers", cellTowersArray); 
 

 
       JSONArray wifiAccessPointsArray = new JSONArray(); 
 
       JSONObject wifiAccessPointObject = new JSONObject(); 
 
       wifiAccessPointObject.accumulate("macAddress", "01:23:45:67:89:AB"); 
 
       wifiAccessPointObject.accumulate("age", 0); 
 
       wifiAccessPointObject.accumulate("channel", 11); 
 
       wifiAccessPointObject.accumulate("signalToNoiseRatio", 40); 
 
       wifiAccessPointsArray.put(wifiAccessPointObject); 
 
       jsonObject.accumulate("wifiAccessPoints", wifiAccessPointsArray); 
 

 
       // 4. convert JSONObject to JSON to String 
 
       json = jsonObject.toString(); 
 

 

 
       // 5. set json to StringEntity 
 
       StringEntity se = new StringEntity(json);; 
 

 
       // 6. set httpPost Entity 
 
       httpPost.setEntity(se); 
 

 
       // 7. Set some headers to inform server about the type of the content 
 
       httpPost.setHeader("Accept", "application/json"); 
 
       httpPost.setHeader("Content-type", "application/json"); 
 

 
       // 8. Execute POST request to the given URL 
 
       HttpResponse httpResponse = httpclient.execute(httpPost); 
 

 
       // 9. receive response as inputStream 
 
       inputStream = httpResponse.getEntity().getContent(); 
 

 
       // 10. convert inputstream to string 
 
       if(inputStream != null) 
 
        result = convertInputStreamToString(inputStream); 
 
       else 
 
        result = "Did not work"; 
 
      } 
 
      catch (MalformedURLException e) { 
 
      \t Log.e("YOUR_APP_LOG_TAG1", "", e); 
 
      } 
 
      catch (IOException e) { 
 
      \t Log.e("YOUR_APP_LOG_TAG2", "", e); 
 
      } 
 
      catch (Exception e) { 
 
      \t Log.e("YOUR_APP_LOG_TAG3", "", e);; 
 
      } 
 

 
      return result; 
 
     } 
 

 
     @Override 
 
     protected void onPostExecute(String result) { 
 
      Log.d("result", result); 
 
      
 
     } 
 
    
 

 
    private static String convertInputStreamToString(InputStream inputStream) throws IOException{ 
 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));; 
 
      String line = ""; 
 
      String result = ""; 
 
      while((line = bufferedReader.readLine()) != null) 
 
       result += line; 
 

 
      inputStream.close(); 
 
      return result; 
 
    } 
 
\t

但I M仍然得到403錯誤,允許的IP是不是配置

諮詢/建議是appreciated.thanks

回答

1

我搞清楚我們自己。如果您在使用Google gelocation Api時遇到問題並獲取403錯誤。那麼你必須遵循這些步驟。這是簡單的:)

  1. 降低沒有API請求0
  2. 然後禁用地理位置API
  3. 創建一個新項目
  4. 然後啓用谷歌地理位置API
  5. 添加/增加沒有的API請求
  6. 創建了Android API密鑰
  7. 服務器API密鑰
  8. 使用它

,你是好走,現在你可以使用你的谷歌地理位置API輕鬆

希望這將有助於 感謝