Android Google 地理位置 api return 403 错误
Android Google Geolocation api return 403 error
我正在尝试构建 android 可以从 Google 地理定位 Api 获取用户位置的应用程序。我配置和设置结算帐户
我的设置是
- 将配额增加到 100/request/user
- 结算帐户设置
- 允许的 ip : 任何
我正在使用异步任务 class
// 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 不是配置
帮助/建议是appreciated.thanks
我认为这是我们自己。如果您在使用 Google 地理定位 Api 时遇到问题并收到 403 错误。那么你必须按照这些步骤。很简单:)
- 将API请求的数量减少到0
- 然后禁用地理定位 API
- 创建一个新项目
- 然后启用 Google 地理定位 Api
- 添加/增加API
的请求数
- 创建 Android API 密钥
- 服务器API键
- 使用它
一切顺利,现在您可以轻松使用 google 地理定位 api
希望对您有所帮助
谢谢
我正在尝试构建 android 可以从 Google 地理定位 Api 获取用户位置的应用程序。我配置和设置结算帐户
我的设置是
- 将配额增加到 100/request/user
- 结算帐户设置
- 允许的 ip : 任何
我正在使用异步任务 class
// 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 不是配置
帮助/建议是appreciated.thanks
我认为这是我们自己。如果您在使用 Google 地理定位 Api 时遇到问题并收到 403 错误。那么你必须按照这些步骤。很简单:)
- 将API请求的数量减少到0
- 然后禁用地理定位 API
- 创建一个新项目
- 然后启用 Google 地理定位 Api
- 添加/增加API 的请求数
- 创建 Android API 密钥
- 服务器API键
- 使用它
一切顺利,现在您可以轻松使用 google 地理定位 api
希望对您有所帮助 谢谢