views.py需要停止一秒钟的googlemap api附近搜索吗?

问题描述 投票:-1回答:1

我使用googlemap API程序创建。我没有在开发中限制api。

enter image description here我将程序部署到服务器,然后打开api限制。该程序看起来有点慢,有时会错过响应。

我需要暂停一秒钟的程序吗?还是设置不好?

google map api document

def nearbysearch(apikey, place, centerlatlng, types, radius):
    '''
    dependency
    ----------
    Places API

    parameters
    ----------
    place: tokyo

    return
    --------
    a place\n
    place_id, rating\n
    e.g. CmRaAAAARRAYThPn0sTB1aE-Afx0_..., 4
    '''
    url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' \
        'location={}&radius={}&type={}&keyword={}&' \
        'key={}'
    url = url.format(centerlatlng, radius, types, urllib.parse.quote(place), apikey)
    # print("nearbysearch:", url)
    res = urllib.request.urlopen(url)

    ////////////// time.sleep(1) ////////////// <- need??

    retvalue = None
    if res.code == 200:
        res_json = json.loads(res.read())
        if res_json.get("results"):
            retvalue = res_json["results"]
    return retvalue

查询:涩谷的拉面日语

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=35.6557695,139.7017417&radius=1500&type=restaurant&keyword=%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3&key=MY_API_KEY

缺少响应:

{
   "error_message" : "API keys with referer restrictions cannot be used with this API.",
   "html_attributions" : [],
   "results" : [],
   "status" : "REQUEST_DENIED"
}

设置:localhost在限制时不起作用

http://127.0.0.1:8000/*
http://www.henojiya.net/*

enter image description here网站:

http://www.henojiya.net/gmarker/

使用了大约200次。enter image description here

python-3.x google-maps google-places-api django-3.0
1个回答
0
投票

您正在使用附近搜索API Web服务,该服务无法与受HTTP引荐来源网址限制的API密钥一起使用。您需要第二个API密钥,受IP地址限制。

此外,如果您的API密钥在生产环境中公开使用,请不要将其限制为本地主机,因为这意味着任何人都可以从自己的本地主机使用它(并在您的帐户上产生费用)。

请查看以下相关答案以了解更多详细信息:Google Maps API is never satisfiedGoogle Play Security alert about Google Cloud Platform (GCP) API keys

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.