如何使用带有受限 API 密钥的 Google-Directions-Android 库 [英] How to use Google-Directions-Android library with a restricted API key

查看:29
本文介绍了如何使用带有受限 API 密钥的 Google-Directions-Android 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,正在从事谷歌地图项目.为此,我使用 this 库来绘制两点之间的路线.

I am new to Android and working on a google map project. For this I am using this library to draw a route between two points.

当对 google map api 密钥没有限制时,这可以正常工作.但是当限制应用于我的应用程序的包名称和 sha1 密钥时,它不会显示路线.

This works fine when there is no restriction on the google map api key. But when restriction is applied to my app's package name and sha1 key it won't show the route.

它抛出一个异常说:

此 IP、站点或移动应用程序无权使用此 API 密钥.从 IP 地址 XX.XX.XXX.XXX 收到请求,引用地址为空

This IP, site or mobile application is not authorized to use this API key. Request received from IP address XX.XX.XXX.XXX, with empty referrer

有什么办法可以通过受限的 api 密钥使用这个库吗?如何使用此库添加引荐来源网址?

Is there any way to use this library with a restricted api key? How can I add a referrer with this library?

推荐答案

看起来这个库使用了 Directions API 网络服务,因此您不能使用受 Android 限制的 API 密钥.如果这样做,您会收到您报告的 request_denied 错误.这是因为 Web 服务是服务器端的,API 密钥只能通过 IP 地址进行限制.

It looks like this library uses the Directions API web service, so you cannot use an Android-restricted API key. If you do, you get the request_denied error you're reporting. This is because web services are server-side and the API key can only be restricted by IP address.

话虽如此,您正在从 Android 应用调用 API.Google 的文档指出:

Having said that, you are calling the API from an Android app. Google's documentation states:

IP 限制可能不切实际,例如在移动应用程序中和依赖动态 IP 地址的云环境.使用时在这些场景中使用 Maps Web Service API,使用一个来保护您的应用程序或更多以下技术:

IP restrictions might be impractical, such as in mobile applications and cloud environments that rely on dynamic IP addresses. When using Maps Web Service APIs in these scenarios, secure your apps using one or more of the following techniques:

固定、代理服务器、混淆

pinning, proxy server, obfuscation

因此,设置代理服务器可能是正确保护 API 密钥的最佳方式.

Hence, setting up a proxy server may be the best way to properly secure your API key.

希望这能澄清您的问题.

Hope this clarifies your question.

这篇关于如何使用带有受限 API 密钥的 Google-Directions-Android 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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