简体   繁体   English

如何使用带有受限 API 密钥的 Google-Directions-Android 库

[英]How to use Google-Directions-Android library with a restricted API key

I am new to Android and working on a google map project.我是 Android 新手,正在从事谷歌地图项目。 For this I am using this library to draw a route between two points.为此,我使用这个库来绘制两点之间的路线。

This works fine when there is no restriction on the google map api key.当对 google map api 密钥没有限制时,这可以正常工作。 But when restriction is applied to my app's package name and sha1 key it won't show the route.但是当限制应用于我的应用程序的包名称和 sha1 键时,它不会显示路线。

It throws an exception saying:它抛出一个异常说:

This IP, site or mobile application is not authorized to use this API key.此 IP、站点或移动应用程序无权使用此 API 密钥。 Request received from IP address XX.XX.XXX.XXX, with empty referrer从 IP 地址 XX.XX.XXX.XXX 收到请求,引用地址为空

Is there any way to use this library with a restricted api key?有什么办法可以通过受限的 api 密钥使用这个库吗? How can I add a referrer with this library?如何使用此库添加引荐来源网址?

It looks like this library uses the Directions API web service , so you cannot use an Android-restricted API key.看起来这个库使用了 Directions API web service ,所以你不能使用 Android 限制的 API 密钥。 If you do, you get the request_denied error you're reporting.如果你这样做,你会得到你报告的 request_denied 错误。 This is because web services are server-side and the API key can only be restricted by IP address.这是因为 Web 服务是服务器端的,API 密钥只能受 IP 地址限制。

Having said that, you are calling the API from an Android app.话虽如此,您正在从 Android 应用程序调用 API。 Google's documentation states:谷歌的文档指出:

IP restrictions might be impractical, such as in mobile applications and cloud environments that rely on dynamic IP addresses. IP 限制可能不切实际,例如在依赖动态 IP 地址的移动应用程序和云环境中。 When using Maps Web Service APIs in these scenarios, secure your apps using one or more of the following techniques:在这些场景中使用 Maps Web Service API 时,请使用以下一种或多种技术保护您的应用程序:

pinning, proxy server, obfuscation固定、代理服务器、混淆

Hence, setting up a proxy server may be the best way to properly secure your API key.因此,设置代理服务器可能是正确保护 API 密钥的最佳方式。

Hope this clarifies your question.希望这能澄清你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM