简体   繁体   English

Google Places API返回的提供的API密钥无效

[英]Google Places API returning provided API key is invalid

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. 阅读有关该主题的所有帖子,并尝试所有组合:1.在进行Google Places API调用时使用了android键(2.切换到浏览器键3.稍后尝试使用服务器键。4.重新生成键并尝试了1-3的组合。

Nothing is working !!. 什么都没用! My key for the Mapv2 API in the manifest file is the android key. 清单文件中Mapv2 API的密钥是android密钥。 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. 我为此包重新创建了新的android密钥。 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. 所以现在我有了一个新项目的Android的新密钥,浏览器。 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 . 当我使用浏览器密钥从浏览器进行此调用时,收到错误消息为“提供的API密钥无效”...。 Not not from my android app . 不是不是从我的Android应用程序。 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 这是我收到错误消息的地方:-提供了APi密钥

Here is my code for the googlePlaceApi in CSharp 这是我在CSharp中的googlePlaceApi的代码

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 api键在这里 在此处输入图片说明

Hope this can help you ;) 希望这可以帮到你 ;)

Follow these instructions in detail. 详细遵循这些说明。 Remember you will need to create Android Key (not Server key) 请记住,您将需要创建Android密钥(而非服务器密钥)

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; }
}

You must have enabled 'Google Places API for Android' and not 'Google Places API for web services' in google console. 您必须在Google控制台中启用了“适用于Android的Google Places API”而不是“适用于网络服务的Google Places API”。 'Google Places API for Web Services' has to enabled if you are using the places web service from android. 如果您使用的是来自Android的Places网络服务,则必须启用“用于Web服务的Google Places API”。 The Key will be a browser key. 密钥将是浏览器密钥。

eg https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=API_KEY 例如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 Google Places API for Web Services-屏幕截图

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

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