简体   繁体   English

为什么我的api密钥不被Google Directions(Android)请求接受?

[英]Why is my api key not accepted by the Google Directions (Android) request?

Whenever I include my Google Play Services API key along with my request for Google Directions like in the code below: 每当我将Google Play服务API密钥与我的Google路线请求一起提供时,请参阅以下代码:

    private String makeDirectionsURL(double originLat, double originLong, double destLat, double destLong)
{
    StringBuilder url = new StringBuilder();
    //first part of url//
    url.append("https://maps.googleapis.com/maps/api/directions/json?");
    //start adding parameters//
    //origin coordinates
    url.append("origin="+originLat+","+originLong);
    //destiniation coordinates
    url.append("&destination=");
    url.append(destLat+","+destLong);
    //api key
    url.append("&key=");
    url.append(getResources().getString(R.string.google_api_key));
    //NOTE: for some reason, the request suceeds when leaving out the api key 

    return url.toString();

}

When I include the api key as a parameter in the request, the json response shows that my request has been denied. 当我在请求中包含api密钥作为参数时,json响应显示我的请求已被拒绝。 The response reads: 回复如下:

{ "error_message" : "This IP, site or mobile application is not authorized to use this API key.", "routes" : [], "status" : "REQUEST_DENIED" } {“error_message”:“此IP,站点或移动应用程序无权使用此API密钥。”,“routes”:[],“status”:“REQUEST_DENIED”}

Yet the same api key works for my google map view - and my api console registers the accesses for the map view quota limit. 然而,相同的api密钥适用于我的谷歌地图视图 - 我的api控制台注册了地图视图配额限制的访问。 Even odder yet, when I leave out the key parameter in the Directions request I get a valid JSON response. 甚至更奇怪,当我在Directions请求中省略key参数时,我得到一个有效的JSON响应。

I have a feeling that I have to generate another different api key for just the Google Directions service - but I'm not sure how to. 我有一种感觉,我必须为Google Directions服务生成另一个不同的api密钥 - 但我不知道如何。 The documentation here: https://developers.google.com/maps/documentation/directions/#api_key 此处的文档: https//developers.google.com/maps/documentation/directions/#api_key

Says to visit the console and activate the Directions API service - I did. 说访问控制台并激活Directions API服务 - 我做到了。 Then it says my api key "will be available from the API Access page, in the Simple API Access section. Directions API applications use the Key for server apps." 然后它说我的api密钥“将在API访问页面的Simple API访问部分提供。方向API应用程序使用密钥用于服务器应用程序。” Now is where I'm confused - how can I use the key for server apps if I am accessing from a mobile device - I'm assuming this is for any webpages that wish to use the service - but what do I do for an android app. 现在是我困惑的地方 - 如果我从移动设备访问我如何使用服务器应用程序的密钥 - 我假设这是任何希望使用该服务的网页 - 但我该怎么做的android应用程序。 As I said, I already tried using my Simple API Access key for Android apps, which I know works, yet when I pass the same key to Google Directions - it mysteriously doesn't work... 正如我所说的,我已经尝试使用我的简单API访问密钥用于Android应用程序,我知道它可以工作,但是当我将相同的密钥传递给Google方向时 - 它神秘地不起作用......

Any help, vague guidance, or links to read up on would be really appreciated. 任何帮助,含糊的指导或阅读链接都将非常感激。

PS: If I can't figure this out - am I allowed to keep sending requests w/oa api key? PS:如果我无法解决这个问题 - 我是否可以继续发送带有api密钥的请求?

I think the issue is because you are trying to pass a server key which is directly tied to your server's IP address, when you should be passing an android app key . 我认为问题是因为你试图传递一个server key ,它直接绑定到你服务器的IP地址,当你应该传递一个android app key

In your Google APIs console, navigate to API's and Auth . 在您的Google API控制台中,导航到API和Auth From here you can create a new public API key. 从这里,您可以创建一个新的公共API密钥。

创建Android密钥

When prompted, select Android Application 出现提示时,选择Android Application 在此输入图像描述

You will need to enter your device's SHA1 Certificate Fingerprint 您需要输入设备的SHA1证书指纹

This comes from the keystore used to sign the apk. 这来自用于签署apk的密钥库。 When running an app in debug, you will most likely be using your IDE's debugging keystore. 在调试中运行应用程序时,您很可能正在使用IDE的调试密钥库。 Google has a nice write up on how to get this information. 谷歌对如何获取这些信息有一个很好的写作

When releasing the app, you will need to sign it with your own key. 发布应用程序时,您需要使用自己的密钥进行签名。 You will either need to edit your API key to the new keystore SHA1 fingerprint or create a new API key. 您需要将API密钥编辑为新的密钥库SHA1指纹或创建新的API密钥。

Additionally, according to Google , you should be using a key. 此外,根据Google的说法,您应该使用密钥。

All Directions API applications should use an API key. 所有Directions API应用程序都应使用API​​密钥。 Including a key in your request: 在您的请求中包含密钥:

  • Allows you to monitor your application's API usage in the APIs 允许您监视API中应用程序的API使用情况
  • Console. 安慰。 Enables per-key instead of per-IP-address quota limits. 启用每个密钥而不是每个IP地址配额限制。
  • Ensures that Google can contact you about your application if necessary. 确保Google可以在必要时就您的申请与您联系。

While I can't comment on how strictly they enforce this, I strongly encourage you to do so to avoid any issues down the road. 虽然我无法评论他们如何严格执行此操作,但我强烈建议您这样做以避免任何问题。

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

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