Google Places API网络服务API密钥无效 [英] Google Places API Web Services API Key Invalid

查看:448
本文介绍了Google Places API网络服务API密钥无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注册了 Google Places API 。他们向我发放了一个服务器密钥,这是我在代码中使用的。



这是我的代码:

  function getLatLon($ address)
{
$ curl = curl_init();
curl_setopt($ curl,CURLOPT_URL,'https://maps.googleapis.com/maps/api/geocode/json?address ='。rawurlencode($ address));
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1);
$ json = curl_exec($ curl);
curl_close($ curl);
$ json = json_decode($ json);
$ details = [
'lat'=> $ json-> results [0] - > geometry-> location-> lat,
'lng'=> $ json-> results [0] - > geometry-> location-> lng
];
返回$ details;
}


函数getDetails($地址)
{
$ apikey ='googlesuppliedapikey';
$ locdetails = getLatLon($ address);
$ latitude = $ locdetails ['lat'];
$ longitude = $ locdetails ['lng'];
$ curl = curl_init();
curl_setopt($ curl,CURLOPT_URL,'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location = $ latitude,$ longitude& key = $ apikey');
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1);
$ json = curl_exec($ curl);
curl_close($ curl);
$ json = json_decode($ json);
print_r($ json);
}


$地址='3342 Shawnee Avenue大道西棕榈滩,FL 33409';
getDetails($ address);

纬度和经度函数可以正常工作。我的问题在 getDetails函数中。



我目前的回应如下

  stdClass Object 

[error_message] =>提供的API密钥无效
[html_attributions] => Array



[results] => Array



[status] => REQUEST_DENIED

不太确定问题所在。我在5分钟前刚刚创建了这个API密钥。 我也尝试了一个我之前创建的事先键,它提供了相同的答案



无论如何感谢您的帮助。 b $ b

解决方案

您的请求网址不正确。 文档


>地点详细信息请求



地点详情请求是以下表单的HTTP网址:



https://maps.googleapis.com/maps/api / place / details / output?参数
其中输出可以是以下任一值:

json (推荐)以JavaScript对象表示法(JSON)表示输出
xml表示以XML格式输出
某些参数用于启动搜索寻求。按照URL中的标准,所有参数都使用&符号(&)字符分隔。下面是参数及其可能值的列表。



(必需) - 您的应用程序的API密钥。此密钥标识您的应用程序用于配额管理的目的,并使您的应用程序添加的地点可立即提供给您的应用程序。访问Google Developers Console以创建API项目并获取您的密钥。
placeid 或引用(您必须提供其中一个,但不能同时提供):
placeid - 从地方搜索返回的唯一标识地点的文本标识符。有关地点ID的更多信息,请参阅地点标识概述。
reference - 从地方搜索返回的唯一标识地点的文本标识符。注意:该参考现已被弃用,以支持placeid。请参阅此页面上的弃用通知。



I just signed up for the Google Places API. They issued me a server key which I am using in my code.

Here is my code:

    function getLatLon($address)
    {
      $curl=curl_init();
      curl_setopt($curl,CURLOPT_URL,'https://maps.googleapis.com/maps/api/geocode/json?address='.rawurlencode($address));
      curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
      $json=curl_exec($curl);
      curl_close($curl);
      $json=json_decode($json);
      $details=[
         'lat'=>$json->results[0]->geometry->location->lat,
         'lng'=>$json->results[0]->geometry->location->lng
      ];
      return $details;
    }


    function getDetails($address)
    {
      $apikey='googlesuppliedapikey';
      $locdetails=getLatLon($address);
      $latitude=$locdetails['lat'];
      $longitude=$locdetails['lng'];
      $curl=curl_init();
      curl_setopt($curl,CURLOPT_URL,'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$latitude,$longitude&key=$apikey');
      curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
      $json=curl_exec($curl);
      curl_close($curl);
      $json=json_decode($json);
      print_r($json);
    }


    $address='3342 Shawnee Avenue Avenue West Palm Beach, FL 33409';
    getDetails($address);

The latitude and longitude function works fine. My issue is within the getDetails function.

My current response is the following

stdClass Object
(
   [error_message] => The provided API key is invalid.
   [html_attributions] => Array
    (
    )

   [results] => Array
    (
    )

   [status] => REQUEST_DENIED
)

Not quite sure what the issue is. I literally just created this API key 5 minutes ago. I also tried a prior key too which I created yesterday and it provided the same answer

Anyway thanks for the help.

解决方案

Your request URL is incorrect.

From the documentation

Place Details Requests

A Place Details request is an HTTP URL of the following form:

https://maps.googleapis.com/maps/api/place/details/output?parameters where output may be either of the following values:

json (recommended) indicates output in JavaScript Object Notation (JSON) xml indicates output as XML Certain parameters are required to initiate a search request. As is standard in URLs, all parameters are separated using the ampersand (&) character. Below is a list of the parameters and their possible values.

key (required) — Your application's API key. This key identifies your application for purposes of quota management and so that places added from your application are made immediately available to your app. Visit the Google Developers Console to create an API Project and obtain your key. Either placeid or reference (you must supply one of these, but not both): placeid — A textual identifier that uniquely identifies a place, returned from a Place Search. For more information about place IDs, see the place ID overview. reference — A textual identifier that uniquely identifies a place, returned from a Place Search. Note: The reference is now deprecated in favor of placeid. See the deprecation notice on this page.

这篇关于Google Places API网络服务API密钥无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆