有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android谷歌地理定位api返回403错误

我正在尝试构建一个安卓应用程序,可以从谷歌地理定位Api获取用户的位置。I配置和设置计费帐户

我的背景是

  1. 将配额增加到100/请求/用户
  2. 帐单帐户设置
  3. 允许ip:任何

我正在使用异步任务类

// 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) { Log.e("YOUR_APP_LOG_TAG1", "", e); } catch (IOException e) { Log.e("YOUR_APP_LOG_TAG2", "", e); } catch (Exception e) { 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; }

但我仍然收到403错误,允许的Ip不是配置

谢谢你的帮助/建议。谢谢


共 (1) 个答案

  1. # 1 楼答案

    我自己想的。如果您在使用GoogleGeLocationAPI时遇到问题,并出现403错误。然后你必须遵循这些步骤。很简单:)

    1. 将API请求的数量减少到0
    2. 然后禁用地理定位API
    3. 创建新项目
    4. 然后启用Google地理定位Api
    5. 添加/增加API的请求数量
    6. 创建Android API密钥
    7. 服务器API密钥
    8. 使用它

    现在你可以轻松地使用谷歌地理定位api了

    希望能有所帮助 谢谢