【问题标题】:Android Place SDK restricted API key is not workingAndroid Place SDK 受限 API 密钥不起作用
【发布时间】:2021-06-27 17:14:47
【问题描述】:

我知道 place API 需要使用服务器密钥,但我正在使用 place SDK for android。我正在使用受限的 Android API 密钥(使用包名和 SHA1 受限),但它给了我以下错误:

此 IP、站点或移动应用程序无权使用此 API 钥匙。从 IP 地址 203.122.438.42 收到的请求,为空 推荐人

作为参考,我在下面粘贴我的代码:

public PlacesSearchResponse fetch(com.google.maps.model.LatLng location) {
        PlacesSearchResponse request = new PlacesSearchResponse();
        GeoApiContext context = new GeoApiContext.Builder()
                .apiKey(BuildConfig.MAPS_API_KEY)
                .build();
        try {
            request = PlacesApi.nearbySearchQuery(context, location)
                    .radius(5000)
                    .keyword("Shivam")
                    .type(PlaceType.BAKERY)
                    .await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return request;
            //Log.d("==", "=====" + request.results[0].name);
        }
    }

我正在使用的依赖项:

> //To get near by locations
> implementation 'com.google.maps:google-maps-services:0.18.0'
> implementation 'com.squareup.okhttp3:okhttp:4.9.0'

【问题讨论】:

    标签: android google-maps google-places-api


    【解决方案1】:

    您没有使用 Places SDK for Android。据我从您的代码中可以看出,您正在为 Google Maps Web 服务使用 Java 包装器。 Web 服务始终旨在用于服务器端,并且它们接受的唯一限制是对 API 密钥的 IP 地址限制。

    在 github (https://github.com/googlemaps/google-maps-services-java) 中查看这个 Java 库,并注意文档,尤其是“该库的预期用途”部分。它指出

    Google 地图服务的 Java 客户端专为在服务器应用程序中使用而设计。 此库不适合在 Android 应用中使用,因为可能会丢失 API 密钥。

    如果您正在构建移动应用程序,则需要引入代理服务器作为移动应用程序和 Google Maps API 网络服务之间的中介。 Google Maps Services 的 Java 客户端将是作为此类代理服务器基础的绝佳选择。

    因此,如您所见,Google 建议的解决方案是引入一个中间代理服务器,您可以在其中使用该库和一个受中间代理服务器 IP 地址限制的 API 密钥。

    【讨论】:

    • 非常感谢您的详细解答。两个问题: 1. 代理服务器使用受限密钥的任何示例,我可以复制粘贴并按原样使用? 2.有没有其他方法或android SDK来搜索附近的地方?
    猜你喜欢
    • 2015-12-31
    • 2018-02-21
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多