问题
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.
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.
It throws an exception saying:
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
Is there any way to use this library with a restricted api key? How can I add a referrer with this library?
回答1:
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.
Having said that, you are calling the API from an Android app. Google's documentation states:
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
Hence, setting up a proxy server may be the best way to properly secure your API key.
Hope this clarifies your question.
来源:https://stackoverflow.com/questions/57196465/how-to-use-google-directions-android-library-with-a-restricted-api-key