Google Places API Key for Android App - android

I have created an API Key (for Android apps) for my Android Application. It was required by google maps API. But now the same key won't work for Google Places API.
I also tried hitting the browser with the following URL:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=my_key
I get:
{
"error_message" : "This IP, site or mobile application is not authorized to use this API key.",
"html_attributions" : [],
"status" : "REQUEST_DENIED"
}
When I googled for an answer, I found this:
https://developers.google.com/places/training/autocomplete-android
What type of API key?
You'll need your own browser or server API key for the Google Places API before you can begin using the Place Autcomplete service. Note that an Android API key will not work for the Place Autocomplete service.
This got me confused as I do not have any URL for a web app, nor any server IP. Therefore I am confused on how to get either a Browser key or a Server key
Could anyone please help me in figuring out what sort of key is needed here? Also if I have to create a Browser key or a Server key, how can I do so?

For working with places api generate browser key because for that android key does not work.
Had same issue but this solved the problem

You need to enable the following places api
-Google Places API Web Service
And you are required to use server key for fetching the data of places.
To get a server key follow the below steps: APIs & Auth -> Credentials -> create new key -> Server key
Browser key will not work.
I had the same issue and I resolved it by doing the same.
Here you can have a look to build your Url
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + latitude + "," + longitude);
googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
googlePlacesUrl.append("&types=" + type);
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key=" + SERVER_KEY);

If you want to integrate Google Place Api in android project and you need a key, then just click the link below it will automatically create a key for
you and just copy that key in your project
https://developers.google.com/places/android-api/start

Related

How to get GoogleMap v2 server key in android?

I need to show nearby places in my GoogleMap. Now I am using api key for getting nearby places. But it throws error. So how to get server key in updated Google api console screen. In that there is no option like "server key". Anybody have an idea just share your thoughts. Thanks
I am using the below code:
https://maps.googleapis.com/maps/api/place/search/json?location="
+ latitude
+ ","
+ +longitude
+ "&radius=3446&types=hospital&"
+ "key="apikey""+"&sensor=true
you have to register your project on google's developer's console. here is the link for the same and there you will find your server key.
Hope this helps you
https://console.developers.google.com/start
You can generate Server Key in the Developer's Console. Go to API Manager > Credentials > Create credentials > API Key > Server Key.
You can also refer/follow Get a key for Google Places API Web service which is the API for place search you are using.

Getting : ipRefererBlocked on android app

I'm trying to use the "Google Identity Toolkit" with the "tutorial" android demo app, but now I get this error:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "ipRefererBlocked",
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
"extendedHelp": "https://console.developers.google.com"
}
],
"code": 403,
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
}
}
Any Idea? In the Google console I need allow some thing? Where?
OAuth 2.0 client IDs:
Android,
Web application
And on Identity Toolkit API console, it allow I select "Web application" client ID in Google provider.
And I have set up this in my "tutorial" app
This error is usually caused when you are trying to run the code on a URL you didn't add to the referrer URLs in the console.
To fix this go to the Google Developer Console, then under API Manager click credentials and find your browser key. On this page you can add any URLs that will be using that key.
Remember it treats www. differently, so to cover all possible pages you could do this:
.domain.com/
domain.com/*
I figured out today that there is a bug when you create a new Android API key. You will have probably noticed that there was also a Browser API key that was generated when you created the Android API key. My Browser API key had no name, and was therefore not configurable. However, I performed a bit of URL manipulation magic to be able to access and edit my Browser API key. Here's what I did:
1) Click on the name of the Android API key
2) Edit the URL right before the ? such that the number before the ? matches the (0-based) index of the position of your Browser API key. That is, if your browser api key appears second in your list of two API keys, then your Android API key would have been first, at position 0. Therefore the URL would read something like this:
.../key/0?...
So now you want to change it to look like this:
.../key/1?...
3) From here you should be able to change your allowed referrers to
http://yourdomain.com/*
Then, so long as you have your keys set up properly everything should work as expected! Also, note that after you do this once you will then have a name for your browser key, and it will be easily editable in the future!
Best of luck!
You will need to...
Create an Android API Key
Set the package name and SHA-1 of your signing key file(see gitkit guide on how to do that)
Use that API Key for the identitytoolkit.api_key value

Google Place API not working for Android Key

I am using google autocomplete place api.
https://maps.googleapis.com/maps/api/place/autocomplete/json?key="My Android Key"&input=Mg%20Ro&components=country:IN
the key i generated from console is Android-key
The API i enabled in Google API Console are
Google Places API Web Service
Google Places API for Android
Google Maps Android API
and SHA-1 finger print are of debug-keystore.
Now on hitting, I'm getting the output for this link is
{"error_message":"This IP, site or mobile application is not authorized to use this API key. Request received from IP address 122.177.199.201, with empty referer","predictions":[],"status":"REQUEST_DENIED"}
While in case of Server-Key i'm getting proper response
{"predictions":[{"description":"MG Road Metro Station","id":"9db78081a8f0e59874d51baff0df2c007e8c8dde","matched_substrings":[{"length":2,"offset":0}],"place_id":"ChIJY8G_aiIZDTkRohS7EKRePsw","reference":"CjQkAAAAj-nav4S-WWDQfmK7D5V_uc6XT8s5wZzsKNLQMHA7ucgRD_AGu5uIoxvWlxtpJaAiEhDOj61RTKbD9lK28Seovch6GhSPoMA7WPxS6v8ApRf_q03TtnzzrQ","terms":[{"offset":0,"value":"MG Road Metro Station"}],"types":["transit_station","point_of_interest","establishment","geocode"]},{"description":"M G Road Metro Station, Mehrauli-Gurgaon Road, Sector 28, Gurgaon, Haryana, India","id":"041408b314da313beac2e2016c697a9aba451be8","matched_substrings":[{"length":22,"offset":0}],"place_id":"ChIJATH6EiIZDTkR-aEggPmaDrU","reference":"CmRgAAAAEMJLJnkGwPSnN5BmE7_IU_UTEAxRT83T2IuHGKja7PouwUxH6j6gVzPlWiSi-yPgYH1vEUs_wiLMME2QbSkOBACPdtQmPdjQ5q-BlLxL3d81UOvaD8-jtvgrKOLLF_dPEhCRDuiBXJ90DQMY7ioJsQxnGhSFULVBF754FuwF2LFwzWTvlxmVoQ","terms":[{"offset":0,"value":"M G Road Metro Station"},{"offset":24,"value":"Mehrauli-Gurgaon Road"},{"offset":47,"value":"Sector 28"},{"offset":58,"value":"Gurgaon"},{"offset":67,"value":"Haryana"},{"offset":76,"value":"India"}],"types":["transit_station","point_of_interest","establishment","geocode"]},{"description":"MGF MAll Management, DLF City Mall Parking Road, Sector 28, Gurgaon, Haryana, India","id":"c8e8961eb24684f8fa6457876e0f5c1d7c38b5b9","matched_substrings":[{"length":2,"offset":0}],"place_id":"ChIJS3fMbyIZDTkRkexzrN-6sBg","reference":"CnRhAAAApKaoK6e148xwbkJZGhNXNTTDqStbf3vU5b0udThzC0jcdbtJceqWXcQ-I8U_x838GYjHTLLcNNswfrTu3jYVgnA-evtNfhN1pRKnugpvOAtonL2aEYmxGHQadW3bY8uzf-ERjosRBicEb2HfdoKZPhIQvUrFQqXv67CoiaWqlVqXUxoUBccSCK3csfVr1HA0H2HZOiqEmu8","terms":[{"offset":0,"value":"MGF MAll Management"},{"offset":21,"value":"DLF City Mall Parking Road"},{"offset":49,"value":"Sector 28"},{"offset":60,"value":"Gurgaon"},{"offset":69,"value":"Haryana"},{"offset":78,"value":"India"}],"types":["establishment"]},{"description":"MGF Infratech, Gurgaon, Haryana, India","id":"1b4b1ce2e29a7a8f3b3d07192bf06f7e6f0df84f","matched_substrings":[{"length":2,"offset":0}],"place_id":"ChIJTytQ6hgZDTkR2p2nzYt9tfA","reference":"CkQ1AAAAgAsxqmineUN1diGhpF5khDroggPvicHj5VlegMg-SYKf3ORdoFU5JB9dp5lpRY5byp9LAIx3alsDZ0k4D5QCrxIQXi5gMF1JakSfbAufPMlvcRoUqMmrZiUzEmhdiVxMBneGKFLrmZg","terms":[{"offset":0,"value":"MGF Infratech"},{"offset":15,"value":"Gurgaon"},{"offset":24,"value":"Haryana"},{"offset":33,"value":"India"}],"types":["establishment"]},{"description":"MG Road, National Highway 236, Maruti Housing Colony, Gurgaon, Haryana, India","id":"20c271b151605328170192b769e72e3e9bee6056","matched_substrings":[{"length":2,"offset":0}],"place_id":"ChIJrYoVEyIZDTkRcmM4_rRo_OE","reference":"CmRcAAAAtEh471bANMlFV-mDp2hOSVxW0_XxmNFQEmGXCiuZxvjsLvOcoyMPC0VhOifD7ngb-2rs5cfplaomYupeARu3kKEf6WPBQ170Ge7ZRNscQ1PDx12EvHkm4R7MrUD2F_kZEhBq6yvt7WzJgAkdXholeEObGhTuXqJl41XcSz2dzzksTMsHsU7Nzg","terms":[{"offset":0,"value":"MG Road"},{"offset":9,"value":"National Highway 236"},{"offset":31,"value":"Maruti Housing Colony"},{"offset":54,"value":"Gurgaon"},{"offset":63,"value":"Haryana"},{"offset":72,"value":"India"}],"types":["transit_station","point_of_interest","establishment","geocode"]}],"status":"OK"}
I'm unable to understand the scenario that why place api are not working for Android-Key. Please anyone try to explain me.
Thanks
First, try to change existing Android key options or create a new key without package name and fingerprint restriction. If it will work, then you are probably have error in you package name ( .debug suffix is added automatically to package name in debug build )
Also make sure you've added your Android key in your manifest
For google places api while hitting api it required server key. Create server key with your sha1 fingerprint and use that key for google places api. And in manifest use api key there. Dont get confused api key and server key.
use android key in manifest and server key for places api. Afetr creating SERVER key wait for 5 minutes. Then your server key will be enabled
StringBuilder sb = new StringBuilder(PLACES_API_BASE+TYPE_AUTOCOMPLETE+OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&types=(cities)");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
here you have to use API_KEY is SERVER key NOT ANDROID key

android-reverse geocoding-Google Map API (geocoding)

I'm developing android app where I use google reverse geocoding API webservice in the user side, for the user to read his address in text format.
HttpGet httpGet = new HttpGet(
"http://maps.googleapis.com/maps/api/geocode/json?latlng="
+ lat + "," + lng + "&sensor=true");
I have the following three questions:
1-Do I have to use a map in my app?, as I'm only displaying the location in text to User and I don't want a map in my app?
https://developers.google.com/maps/terms#section_10_1
"(h) No Use of Content without a Google Map. You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation"
2-is it a must that I include API key at app side part of calling the webservice?
3-am I expected to establish enterprise agreement with Google, and get client_ID and include it in the request sent by the end user?
https://developers.google.com/maps/terms#section_9_1
To answer the questions:
1) I think you can use this without using the maps, for, in the API doc it said:
The Google Geocoding API provides a direct way to access a these services via an HTTP request.
https://developers.google.com/maps/documentation/geocoding/#Geocoding
2) You need an API key for using the service, even it works without using one (not recommended). Refer to this section for more information.
All Maps API applications* should load the Maps API using an API key. Using an API key enables you to monitor your application's Maps API usage, and ensures that Google can contact you about your application if necessary.
https://developers.google.com/maps/documentation/javascript/tutorial#api_key
3) for client_id, I think you don't need to unless you have to..
https://developers.google.com/maps/documentation/business/

Why is my api key not accepted by the Google Directions (Android) request?

Whenever I include my Google Play Services API key along with my request for Google Directions like in the code below:
private String makeDirectionsURL(double originLat, double originLong, double destLat, double destLong)
{
StringBuilder url = new StringBuilder();
//first part of url//
url.append("https://maps.googleapis.com/maps/api/directions/json?");
//start adding parameters//
//origin coordinates
url.append("origin="+originLat+","+originLong);
//destiniation coordinates
url.append("&destination=");
url.append(destLat+","+destLong);
//api key
url.append("&key=");
url.append(getResources().getString(R.string.google_api_key));
//NOTE: for some reason, the request suceeds when leaving out the api key
return url.toString();
}
When I include the api key as a parameter in the request, the json response shows that my request has been denied. The response reads:
{
"error_message" : "This IP, site or mobile application is not authorized to use this API key.",
"routes" : [],
"status" : "REQUEST_DENIED"
}
Yet the same api key works for my google map view - and my api console registers the accesses for the map view quota limit. Even odder yet, when I leave out the key parameter in the Directions request I get a valid JSON response.
I have a feeling that I have to generate another different api key for just the Google Directions service - but I'm not sure how to. The documentation here:
https://developers.google.com/maps/documentation/directions/#api_key
Says to visit the console and activate the Directions API service - I did.
Then it says my api key "will be available from the API Access page, in the Simple API Access section. Directions API applications use the Key for server apps." Now is where I'm confused - how can I use the key for server apps if I am accessing from a mobile device - I'm assuming this is for any webpages that wish to use the service - but what do I do for an android app. As I said, I already tried using my Simple API Access key for Android apps, which I know works, yet when I pass the same key to Google Directions - it mysteriously doesn't work...
Any help, vague guidance, or links to read up on would be really appreciated.
PS: If I can't figure this out - am I allowed to keep sending requests w/o a api key?
I think the issue is because you are trying to pass a server key which is directly tied to your server's IP address, when you should be passing an android app key.
In your Google APIs console, navigate to API's and Auth. From here you can create a new public API key.
When prompted, select Android Application
You will need to enter your device's SHA1 Certificate Fingerprint
This comes from the keystore used to sign the apk. When running an app in debug, you will most likely be using your IDE's debugging keystore. Google has a nice write up on how to get this information.
When releasing the app, you will need to sign it with your own key. You will either need to edit your API key to the new keystore SHA1 fingerprint or create a new API key.
Additionally, according to Google, you should be using a key.
All Directions API applications should use an API key. Including a key
in your request:
Allows you to monitor your application's API usage in the APIs
Console. Enables per-key instead of per-IP-address quota limits.
Ensures that Google can contact you about your application if necessary.
While I can't comment on how strictly they enforce this, I strongly encourage you to do so to avoid any issues down the road.

Categories

Resources