【问题标题】:REQUEST DENIED with Google Places API KEY for Server Web used in an Android application在 Android 应用程序中使用的用于服务器 Web 的 Google Places API KEY 请求被拒绝
【发布时间】:2015-06-23 18:58:10
【问题描述】:

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

其次,我为浏览器创建了一个 API KEY。由于另一个 API,我已经有一个 API KEY Server。

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

{

"error_message" : "此服务需要 API 密钥。",

“html_attributions”:[],

“结果”:[],

“状态”:“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 Key 和用于浏览器应用程序的 Api Key,我得到了相同的结果。

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

【问题讨论】:

    标签: java android google-places-api


    【解决方案1】:

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

    Here 是使用Google Places API for Android 的示例,here 是使用Google Places API Web Service 的示例。你肯定用的是后者。

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

    如果您在登录 Google Cloud Console 帐户时访问此链接: https://console.cloud.google.com/apis/library?filter=category:maps

    这是应该启用的 API:

    【讨论】:

    • 谢谢@Daniel Nugent!!!我启用了我的 Google Places API Web 服务,但......仍然无法正常工作!!!我认为你是对的,我必须使用 Google Places API Web Service,也许我的代码有问题。为了进行测试,我使用了 API Key Server 和 API Key Browser
    • 请注意,文档说您需要使用服务器密钥,但我已将它与浏览器密钥一起使用,并且它也有效。这是文档:developers.google.com/places/webservice/intro
    • 耶!!!我已经在我的浏览器上使用我的 API KEY 服务器启动了这个 url maps.googleapis.com/maps/api/place/nearbysearch/… 并且它可以工作!!!但是,如果我尝试通过我的 Android 应用程序获取信息,它就不起作用!!! :(
    • 错误相同:{ "error_message" : "此服务需要 API 密钥。", "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" }
    • 已解决!!!我已经修改了从 Google Places API Web 服务下载数据的方法,您推荐的方法 here 你救了我的命!!非常感谢@Daniel Nugent!!!
    【解决方案2】:

    来自文档:

    注意:您需要 Android API 密钥,而不是浏览器密钥。您可以为您的 Google Maps Android API v2 应用和 Google Places API for Android 应用使用相同的 API 密钥。

    查看this 以获得更多帮助。

    【讨论】:

    • 谢谢,但是......我刚刚用 Android API Key 做了一个测试,但不起作用:(
    【解决方案3】:

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

    Enable Google services for your app

    【讨论】:

      猜你喜欢
      • 2012-09-04
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2013-01-17
      • 2016-04-24
      相关资源
      最近更新 更多