【问题标题】:Google Places API returning provided API key is invalid返回提供的 API 密钥的 Google Places API 无效
【发布时间】:2014-05-19 06:29:34
【问题描述】:

通读关于这个主题的所有帖子并尝试所有组合: 1. 调用 google places API 时使用了 android 密钥( 2.切换到浏览器键 3.后来尝试使用服务器密钥。 4.重新生成密钥并尝试组合1-3。

没有任何工作!清单文件中 Mapv2 API 的密钥是 android 密钥。在我创建一个新项目并将我的包与新项目一起列出之前,此应用程序一直正常工作。我为这个包重新创建了新的 android 密钥。旧钥匙仍然在那里,但有一个不同的项目。我没有删除旧项目,但删除了它下面的所有密钥。 所以现在我有一个新项目下的 android 浏览器的新密钥。 我做错了吗?

我收到错误消息“提供的 API 密钥无效”... 当我使用浏览器键从浏览器进行此调用时,它正在工作。不是来自我的 android 应用程序。 有小费吗 ?。

请看下面的代码:-

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

这是我收到错误消息的地方:- 提供的 APi 密钥无效

【问题讨论】:

  • 你设置了正确的api密钥吗?设置 api 位置时出现相同的错误。只需检查您是否拥有正确 api 的正确密钥。 link
  • @dpfauwadel - 是的,我尝试过使用 android 密钥、服务器密钥和浏览器密钥。没有想法了.. :-(
  • 一些帖子谈论使用 android key ,而其他一些帖子谈论使用 browser key 。 google place 文档讨论了使用服务器密钥。我尝试了所有 3 - 但我仍然收到错误消息:“提供的 API 密钥无效”。我希望他们有更好的错误报告来告诉使用哪个键..
  • 尊敬的专家们,这对我来说已经有数周的挫败感了。我重新生成了 android 密钥。有人可以权威地告诉我使用哪个密钥。我相信我已经逐字遵循了许多地方的说法。我也在上面发布了我的代码。
  • 请按照以下清单进行操作: 1) 使用新的 .keystore 为应用程序签名。 2) 应使用新的 .keystore 的 SHA1。 3) 应使用此 SHA1 制作映射密钥。 4) 如果使用新的 Google 帐户,请确保为该帐户打开 Android Maps V2 服务。当 Maps V2 服务未开启时,通常会出现此错误。

标签: android google-places-api


【解决方案1】:

这是我在 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;
    }

Api Key 在这里

希望对你有所帮助;)

【讨论】:

  • 感谢代码/帮助。我没有使用过 xml 响应——也许我会尝试一下。我不认为它应该有任何区别。我已经从开发者控制台为我的项目打开了 Places API。所以这不是问题。
  • 也可以尝试激活 Geocoding API。
  • @dpfauwadel,我启用了所有必需的 API。该代码适用于地图 API v2。当我不得不更改与 google places api 一起使用的旧 google search api 代码时,问题就出现了。你能确认我是否需要使用安卓键或浏览器键? (当然,android 密钥将在清单文件中)。我还看到您使用了我的电话中缺少的 types=establishment。不确定这是否会有所不同。
  • @sunny,您可以尝试使用浏览器密钥。看到这个帖子stackoverflow.com/questions/17715572/…
  • 现在我遇到了一个新问题:- 我收到一个 java filenotfound 错误:-java.io.FileNotFoundException: maps.googleapis.com/maps/api/place/nearbysearch/… joe&key= .
【解决方案2】:

【讨论】:

  • 感谢您的回复 - 我正在使用 android 密钥 - 无论是在清单文件中还是在 google place api 请求的 key 参数中。
  • 我通读了上面的帖子——我没有发现我正在做的事情有什么问题。也许我错过了一些东西,但无法找到。
【解决方案3】:
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; }
}

【讨论】:

    【解决方案4】:

    您必须在 Google 控制台中启用“适用于 Android 的 Google Places API”而不是“适用于 Web 服务的 Google Places API”。如果您使用来自 android 的地点网络服务,则必须启用“用于网络服务的 Google 地点 API”。密钥将是浏览器密钥。

    例如 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      相关资源
      最近更新 更多