1

read thru all the posts on this subject and tried out all the combination : 1. used the android key while making the google places API call ( 2. switched to browser key 3. later tried with server key. 4. re-generated the keys and tried the combination 1-3.

Nothing is working !!. My key for the Mapv2 API in the manifest file is the android key. This app was working correctly until I created a new project and listed my package with the new project. I recreated new android key for this package. The old key was still there but with a different project. I have not deleted the old project but removed all the key under it. so now I have new keys for android, browser under a new project. am I doing it wrong?.

I get the error message as " Provided API key is invalid"... when I make this call from the browser using the browser key it is working . Not not from my android app . any tips ?.

Please see the code below:-

final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place/nearbysearch";
final String OUT_JSON = "/json";
final String KEY=<browser-key>
final String SENSOR="false";
StringBuilder querystring = new StringBuilder(PLACES_API_BASE+OUT_JSON);
try{
querystring.append("?sensor="+SENSOR+"&key="+KEY);
String localquery="&location=37.316318,-122.005916&radius=500&name=traderjoe";
                querystring.append(URLEncoder.encode(localquery, "UTF-8"));
                URL url = new URL(querystring.toString());
                 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
         connection.setRequestMethod("GET");
           String line;
   StringBuilder builder = new StringBuilder();
   BufferedReader reader = new BufferedReader(
     new InputStreamReader(connection.getInputStream()));
   while((line = reader.readLine()) != null) {
    builder.append(line);
}
       System.out.println("JSON BUILDER INPUT FROM GOOGLE PLACES QUERY="+builder.toString());

This is where I get the error message:- Provided APi Key is invaild

sunny
  • 643
  • 2
  • 11
  • 29
  • Have you set the right api key? I get the same error when the setting the api places. Just check you have the right key for the right api. [link](https://developers.google.com/maps/documentation/webservices/?hl=fr) – dpfauwadel Apr 07 '14 at 07:11
  • @dpfauwadel - yes, I had tried with android key, server key and browser key. running out of ideas.. :-( – sunny Apr 08 '14 at 04:24
  • some posts talk about using android key, while some other talk about using browser key . the google place documentation talks about using server key. I tried all 3 - but I still get the error message: "provided API key invalid". i wish they had better error reporting telling which key to use .. – sunny Apr 08 '14 at 04:26
  • Dear experts, it had been weeks of frustration for me. I re-generated the android keys. can someone authoritatively tell me which key to use. I believe I have followed verbatim what has been told at numerous places. I am also posting my code above. – sunny Apr 27 '14 at 01:10
  • Please follow the check-list : 1) Sign the app with new .keystore. 2) SHA1 of new .keystore should be used. 3) Map key should be made using this SHA1. 4) If new Google account is used, then make sure Android Maps V2 services is turned on for that account. This error come usually when Maps V2 services are not turned on. – Prachi Apr 28 '14 at 08:47
  • 1. - did that. used a new keystore. 2. new SHA1 was used to generate the key 3. Mapkey in the manifest file was updated with the new key. 4. I even verified that Mapv2 is enabled for the project. Now if the question is should use the server key or browser key while making the google place query -i even tried that but it did not work. – sunny Apr 29 '14 at 17:22

4 Answers4

1

Here is my code for the googlePlaceApi in CSharp

public List<GooglePlace> GoogleApiPlace(Coordinate startCoordinate)
    {
        List<GooglePlace> ListOfPlace = new List<GooglePlace> ();

        string apiURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=";
        string apiKey = "myKey";

        string linkApi = apiURL + startCoordinate.Latitude.ToString ().Replace (',', '.') + "," + startCoordinate.Longitude.ToString ().Replace (',', '.') + "&radius=1000&sensor=true&types=establishment&key=" + apiKey;

        var request = HttpWebRequest.Create(linkApi);
        request.ContentType = "application/xml";
        request.Method = "GET";

        try
        {
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    Console.Out.WriteLine("Error fetching data, Server returned status code {0}", response.StatusCode);
                }
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    var content = reader.ReadToEnd();
                    if (string.IsNullOrWhiteSpace(content))
                    {
                        Console.Out.WriteLine("Responsed contained empty body...");
                    }
                    else
                    {
                        XmlDocument document = new XmlDocument();
                        document.LoadXml(content);
                        Console.Out.WriteLine(content);
                        XmlNodeList nodeList = document.GetElementsByTagName("result");

                        foreach (XmlNode docNode in nodeList)
                        {
                            string tot = ((XmlElement)docNode).GetElementsByTagName("lat")[0].InnerText;
                            GooglePlace place = new GooglePlace()
                            {
                                Location = new Location()
                                {
                                    Coordinate = new Coordinate()
                                    {
                                        Latitude = Double.Parse(((XmlElement)docNode).GetElementsByTagName("lat")[0].InnerText, CultureInfo.InvariantCulture),
                                        Longitude = Double.Parse(((XmlElement)docNode).GetElementsByTagName("lng")[0].InnerText, CultureInfo.InvariantCulture)
                                    }
                                },
                                Name = docNode.ChildNodes.Item(0).InnerText.TrimEnd().TrimStart(),
                                Vicinity = docNode.ChildNodes.Item(1).InnerText.TrimEnd().TrimStart()
                            };
                            ListOfPlace.Add(place);
                        }
                    }
                }
            }
        }
        catch
        {
            //TODO : Exception
        }

        return ListOfPlace;
    }

the Api Key is here enter image description here

Hope this can help you ;)

dpfauwadel
  • 3,866
  • 3
  • 23
  • 40
  • Thanks for the code/help. I have not used the xml response - maybe I will try with that. I do not think it should make any difference. I have turned on the places API for my project from the developer console. so that is not the issue. – sunny Apr 29 '14 at 17:29
  • Maybe try to activate the Geocoding API too. – dpfauwadel Apr 30 '14 at 07:43
  • @ dpfauwadel, i had all the required API enabled. the code was working for the map API v2. The issues came in when I had to change the old google search api code I was using with google places api. can u please confirm if I need to be using the android key or the browser key ? ( ofcourse, android key will be in the manifest file). I also see that you use types=establishment which is missing in my call. not sure if that would make any difference. – sunny May 02 '14 at 00:18
  • @sunny, you can try to use the browser key. See this post http://stackoverflow.com/questions/17715572/google-api-keys-what-is-server-key-and-browser-key – dpfauwadel May 02 '14 at 14:41
  • now I am getting a new problem :- I get a java filenotfound error:-java.io.FileNotFoundException: https://maps.googleapis.com/maps/api/place/nearbysearch/json?&sensor=false&types=establishment&location=39.245773,-121.959354&radius=500&name=trader joe&key= . – sunny May 04 '14 at 00:23
  • But the code is working now? A FileNotFoundException is that the file you request is missing – dpfauwadel May 05 '14 at 08:45
0

Follow these instructions in detail. Remember you will need to create Android Key (not Server key)

user1406716
  • 9,565
  • 22
  • 96
  • 151
  • Thank you for your response - I am using the android key - both in the manifest file and also in the key argument of the google place api request. – sunny Apr 08 '14 at 04:13
  • I read thru the above post fully - I could not find anything wrong with what I am doing. Maybe I am missing out something but unable to find out. – sunny Apr 27 '14 at 01:29
0
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string jsonString = string.Empty;

        string url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=restaurant&key=[Your_APIKey]";
        using (System.Net.WebClient client = new WebClient())
        {
            jsonString = client.DownloadString(url);
        }

        var valueSet = JsonConvert.DeserializeObject<RootObject>(jsonString);


    }
}


public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
}

public class OpeningHours
{
    public bool open_now { get; set; }
    public List<object> weekday_text { get; set; }
}

public class Photo
{
    public int height { get; set; }
    public List<object> html_attributions { get; set; }
    public string photo_reference { get; set;}
    public int width { get; set; }
}

public class Result
{
    public Geometry geometry { get; set; }
    public string icon { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public OpeningHours opening_hours { get; set; }
    public List<Photo> photos { get; set; }
    public string place_id { get; set; }
    public double rating { get; set; }
    public string reference { get; set; }
    public string scope { get; set; }
    public List<string> types { get; set; }
    public string vicinity { get; set; }
    public int? price_level { get; set; }
}

public class RootObject
{
    public List<object> html_attributions { get; set; }
    public string next_page_token { get; set; }
    public List<Result> results { get; set; }
    public string status { get; set; }
}
Shiraj Momin
  • 665
  • 6
  • 8
0

You must have enabled 'Google Places API for Android' and not 'Google Places API for web services' in google console. 'Google Places API for Web Services' has to enabled if you are using the places web service from android. The Key will be a browser key.

e.g. https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=API_KEY

Google Places API For Web Services - screenshot

Hashir N A
  • 21
  • 3