Reputation: 653
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
Upvotes: 1
Views: 9576
Reputation: 21
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.
Google Places API For Web Services - screenshot
Upvotes: 0
Reputation: 695
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; }
}
Upvotes: 0
Reputation: 3934
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
Hope this can help you ;)
Upvotes: 1
Reputation: 9705
Follow these instructions in detail. Remember you will need to create Android Key (not Server key)
Upvotes: 0