REQUEST DENIED with Google Places API KEY for Server Web used in a Android application

REQUEST DENIED with Google Places API KEY for Server Web used in an Android application

我正在尝试从 Google 个地方的 API 获取信息,用于 Android 应用程序。为此,我首先在我的 Google 帐户中启用了此 API。

其次,我为浏览器创建了一个 API KEY。由于另一个 API.

,我已经有一个 API KEY 服务器

所以,在我的代码中,我已经用这两个密钥进行了测试,并且我总是得到相同的结果!!!

{

"error_message" : "This service requires an API key.",

"html_attributions" : [],

"results" : [],

"status" : "REQUEST_DENIED"

}

我用来拨打电话的代码是...

        @Override
    protected String doInBackground(LocationService... ls) {
        JSONObject result = new JSONObject();
        URL url;
        HttpsURLConnection urlConnection;

        // Making HTTP request
        try {

            //Define connection
            url = new URL("https://maps.googleapis.com/maps/api/place/nearbysearch/json");
            urlConnection = (HttpsURLConnection)url.openConnection();

            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setRequestProperty("charset", "utf-8");
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setUseCaches(false);

            //Send data
            String parameters = "?location=" + String.valueOf(ls[0].getLocation().getLatitude()) + "," + String.valueOf(ls[0].getLocation().getLongitude());
            parameters+="&radius=5000";
            parameters+="&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket";
            parameters+="&sensor=false";
            parameters+="&key=" + Constants.API_KEY_BROWSER_APPLICATIONS;
            byte[] postData = parameters.getBytes(Charset.forName("UTF-8"));
            int postDataLength = postData.length;
            urlConnection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            DataOutputStream data = new DataOutputStream(urlConnection.getOutputStream());
            data.write(postData);
            data.flush();
            data.close();

            Log.d(TAG, "Datos enviados");
            Log.d(TAG, "ResponseCode: " + String.valueOf(urlConnection.getResponseCode()));

            //Display what returns POST request

            StringBuilder sb = new StringBuilder();

            int HttpResult = urlConnection.getResponseCode();

            if(HttpResult == HttpURLConnection.HTTP_OK){
                String json;

                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));

                String line;

                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }

                br.close();

                //System.out.println(""+sb.toString());
                Log.d(TAG, "json: " + sb.toString());

                FileService file = new FileService();
                file.writeLog(POIActivity.TAG, getClass().getName(), POIActivity.urlConnection + parameters);
                file.writeLog(POIActivity.TAG, "doInBackground", sb.toString());

                // Parse the String to a JSON Object
                result = new JSONObject(sb.toString());

            }else{
                //System.out.println(urlConnection.getResponseMessage());
                Log.d(TAG, "urlConnection.getResponseMessage(): " + urlConnection.getResponseMessage());
                result = null;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.d(TAG, "UnsuppoertedEncodingException: " + e.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "Error JSONException: " + e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "IOException: " + e.toString());
        }

        // Return JSON Object
        return result.toString();


    }

当我调用 API 时,我得到了 ResponseCode = 200 并且我构建的调用最终是这样的...

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.26790166666667,-0.7052183333333333&radius=5000&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket&sensor=false&key=API_KEY

记住,就像 API_KEY 我都用过,Api 服务器应用程序的密钥和浏览器应用程序的 Api 密钥,我得到了相同的结果。

真诚地,我对这个问题很绝望,因为我不知道我做错了什么!!!

来自文档:

Note: You need an Android API key, not a browser key. You can use the same API key for your Google Maps Android API v2 apps and your Google Places API for Android apps.

查看 this 以获得更多帮助。

为了更简单的方法,尝试最新的 GCM 配置文件实现并使用他们的开发人员界面轻松创建项目。

Enable Google services for your app

问题是您没有使用 Google Places API for Android,您使用的是 Google Places API Web Service

is an example of using Google Places API for Android, and here 是使用 Google Places API Web Service 的示例。你肯定用的是后者。

启用 Google Places API Web Service 它将起作用:

如果您在登录 Google Cloud Console 帐户后转到此 link: https://console.cloud.google.com/apis/library?filter=category:maps

这是应该启用的API: