简体   繁体   English

使用Android应用程序中用于服务器Web的Google Places API KEY请求拒绝

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

I'm trying to get info from the API of Google Places for an Android application. 我正在尝试从Android应用程序的Google Places API中获取信息。 To do that, first I have enabled this API in my Google Account. 为此,我首先在我的Google帐户中启用了此API。

在此处输入图片说明

在此处输入图片说明

Second, I have created an API KEY for Browser. 其次,我为浏览器创建了一个API KEY。 I already have an API KEY Server due to another API. 由于其他API,我已经有一个API KEY服务器。

在此处输入图片说明

So, in my code I have been tested with these two Keys and with both I've got always the same result!!! 因此,在我的代码中,我已经使用这两个键进行了测试,而我得到的结果始终相同!!!

{ {

"error_message" : "This service requires an API key.", “ error_message”:“此服务需要API密钥。”,

"html_attributions" : [], “ html_attributions”:[],

"results" : [], “结果”:[],

"status" : "REQUEST_DENIED" “状态”:“ REQUEST_DENIED”

} }

The code that I'm using to make the call are ... 我用来拨打电话的代码是...

        @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();


    }

When I make the call to the API I've got like ResponseCode = 200 and the call that I build is finally like that ... 当我对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

Remember, like API_KEY I have used both, Api Key for server applications and Api Key for browser applications and I've got the same result with both. 记住,就像我使用过的API_KEY一样,用于服务器应用程序的Api Key和用于浏览器应用程序的Api Key两者都得到了相同的结果。

Sincerely, I'm desperate with this problem because I don't know what I am doing wrong!!! 真诚的,我对这个问题感到绝望,因为我不知道自己在做什么错!!!

The problem is that you are not using the Google Places API for Android , you are using the Google Places API Web Service . 问题是您没有使用Google Places API for AndroidGoogle Places API for Android ,而是使用了Google Places API Web Service

Here is an example of using Google Places API for Android , and here is an example of using the Google Places API Web Service . 是使用Google Places API for Android的示例, 是使用Google Places API Web Service的示例。 You are definitely using the latter. 您肯定在使用后者。

Enable the Google Places API Web Service and it will work: 启用Google Places API Web Service ,它将起作用:

If you go to this link while signed into your Google Cloud Console account: https://console.cloud.google.com/apis/library?filter=category:maps 如果您在登录Google Cloud Console帐户后转到此链接: https : //console.cloud.google.com/apis/library?filter= category: maps

This is the API that should be enabled: 这是应启用的API:

在此处输入图片说明

From the documentation: 从文档中:

Note: You need an Android API key, not a browser key. 注意:您需要一个Android API密钥,而不是浏览器密钥。 You can use the same API key for your Google Maps Android API v2 apps and your Google Places API for Android apps. 您可以将相同的API密钥用于Google Maps Android API v2应用程序和适用于Android应用程序的Google Places API。

Check this for more help. 检查以获得更多帮助。

For an easier way try latest GCM configuration file implementation and easily create project using their developer interface. 以更简单的方式尝试最新的GCM配置文件实现,并使用其开发人员界面轻松创建项目。

Enable Google services for your app 为您的应用启用Google服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM