简体   繁体   English

即使使用正确的 API 密钥,Android 自动完成功能的 Google Places API 请求也被拒绝

[英]Google Places API request denied for Android autocomplete, even with the right api key

I'm trying to make an Autocomplete field which should fetch cities as the user types, by using the Google Places API as described in this tutorial: https://developers.google.com/places/training/autocomplete-android我正在尝试使用本教程中所述的 Google Places API 创建一个自动完成字段,该字段应该根据用户类型获取城市: https : //developers.google.com/places/training/autocomplete-android

You've probably found this question around many times before as I did, but none of the answers helped me.您之前可能已经像我一样多次发现这个问题,但没有一个答案对我有帮助。 Here are the things you should know:以下是你应该知道的事情:

The URL is https://maps.googleapis.com/maps/api/place/autocomplete/json?sensor=false&key=myKey&components=country:ro&input=whatTheUserTypes网址是https://maps.googleapis.com/maps/api/place/autocomplete/json?sensor=false&key=myKey&components=country:ro&input=whatTheUserTypes

Please don't reply by saying you replaced the API key with your own and it worked - the API key which goes there must be Android specific and won't work from a browser.请不要回复说您用自己的 API 密钥替换了 API 密钥并且它有效 - 那里的 API 密钥必须是 Android 特定的,并且不能在浏览器中使用。

So did I make the Android API key using the SHA1 fingerprint obtained from the keystore I signed the app with.我也是使用从我签署应用程序的密钥库中获得的 SHA1 指纹来制作 Android API 密钥的。 I turned on Maps and Places APIs from the console.我从控制台打开了 Maps and Places API。 The quota isn't exceeded.没有超过配额。

All those and it still gives me REQUEST_DENIED所有这些,它仍然给我 REQUEST_DENIED

What I didn't mention is that I have O2Auth activated - does that change anything?我没有提到的是我激活了 O2Auth - 这会改变什么吗? Shouldn't it be as simple as putting the API key in the app?难道不应该像将 API 密钥放在应用程序中一样简单吗?

Thanks!谢谢!

Although this has been answered, I think the community could do better.虽然这已经得到了回答,但我认为社区可以做得更好。

I was tearing my hair out about this, it just didn't make sense to me.. I was making an iOS/Android App, so I made an iOS/Android Key... Wrong.我正在为此烦恼,这对我来说没有意义......我正在制作一个iOS / Android应用程序,所以我制作了一个iOS / Android Key......错了。

With Google's Places API, your bundle identifier isn't even considered.使用 Google 的 Places API,甚至不会考虑您的包标识符。

What you really want to do is this: (I'm using the new User Interface)您真正想做的是:(我正在使用新的用户界面)

1. Log into https://cloud.google.com/console#/project 1. 登录https://cloud.google.com/console#/project

Select your Project Name, then go into API's & Auth > APIs选择您的项目名称,然后进入API's & Auth > APIs

Make sure you have Places API Turned on.确保您已打开 Places API。 This is the only thing that needs to be turned on for Places-API to work.这是唯一需要打开 Places-API 才能工作的东西。在此处输入图片说明

2. Go into Credentials 2. 进入凭证

Click CREATE NEW KEY under Public API Access单击公共 API 访问下的创建新密钥在此处输入图片说明

3. Select BROWSER KEY 3. 选择浏览器密钥在此处输入图片说明

4. Click Create, Nothing Else 4.点击创建,没有别的

Leave the HTTP Refer box empty.将 HTTP 引用框留空。

在此处输入图片说明

5. Use the Key Generated here 5. 使用此处生成的密钥

This key will allow ANY user from any device access to the API via your Developer login.此密钥将允许任何设备的任何用户通过您的开发人员登录访问 API。 You can try it out here: (Be sure to replace YOUR_KEY_HERE with your generated Key)你可以在这里试试:(一定要用你生成的密钥替换YOUR_KEY_HERE

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Food%20Sh&sensor=false&radius=500&location=0,0&key=YOUR_KEY_HERE

6. Enjoy 6. 享受

Now you can use that URL above in your Android/iOS device.现在您可以在您的 Android/iOS 设备中使用上面的 URL。

The Google Places API does not currently support Android or iOS keys generated from the Google APIs Console. Google Places API 当前不支持从 Google API 控制台生成的 Android 或 iOS 密钥。 Only Server and Browser keys are currently supported.当前仅支持服务器和浏览器密钥。

If you would like to request this support, please file a Places API - Feature Request .如果您想请求此支持,请提交Places API - 功能请求

https://developers.google.com/places/training/autocomplete-android https://developers.google.com/places/training/autocomplete-android

Storing your API key存储您的 API 密钥

Although the above code demonstrates how to communicate directly between an Android app and the Places Autocomplete service, you should not store your Places API key with your app.尽管上面的代码演示了如何在 Android 应用程序和 Places Autocomplete 服务之间直接通信,但您不应将 Places API 密钥存储在您的应用程序中。

You should therefore build a web application that stores your API key and proxies the Places API services.因此,您应该构建一个 Web 应用程序来存储您的 API 密钥并代理 Places API 服务。 In order to secure communication between your Android app and the proxy web service, you should require user authentication to your proxy web service.为了保护您的 Android 应用程序和代理 Web 服务之间的通信,您应该要求对您的代理 Web 服务进行用户身份验证。 Your Android app can securely store user credentials and pass them to your web service, or the user can log into your web app via an Android WebView.您的 Android 应用程序可以安全地存储用户凭据并将其传递给您的 Web 服务,或者用户可以通过 Android WebView 登录您的 Web 应用程序。

For the latter approach, your web app should create and return a user authentication token to your Android app, and your Android app should subsequently pass this token to your proxy web service.对于后一种方法,您的 Web 应用程序应创建用户身份验证令牌并将其返回给您的 Android 应用程序,然后您的 Android 应用程序应将此令牌传递给您的代理 Web 服务。

Go to google cloud platform console> Credentials click on edit by selected your YOUR_API_KEY > Application restrictions > select none option> save thats it.转到谷歌云平台控制台>凭据点击编辑,选择您的YOUR_API_KEY >应用程序限制>选择选项>保存就是这样。

If you select the android apps option from Application restrictions then google deny the place API with exception REQUEST_DENIED .如果您从应用程序限制中选择了android 应用程序选项,那么谷歌会以REQUEST_DENIED异常拒绝地点 API。

In Google dev console, you should be able to find both "Places API" and "Places API for Android"在 Google 开发控制台中,您应该能够找到“Places API”和“Places API for Android”

开发控制台搜索

Make sure to use "Places API for Android"确保使用“Places API for Android”

For some reason, "Places API for Android" is hidden in the API list, but can be accessed using search.出于某种原因,“Places API for Android”隐藏在 API 列表中,但可以使用搜索进行访问。

I had the same issue , I fix it by leaving我有同样的问题,我通过离开来解决它

Accept requests from these HTTP referrers (web sites) (Optional)接受来自这些 HTTP 引用者(网站)的请求(可选)

in browser key Empty在浏览器键为空

I am still new, so I cannot comment, but to shed some light on Moe's answer, I resolved some similar Google Maps API issues regarding URL queries (for directions, using Volley) with the following steps:我还是个新手,所以我无法发表评论,但为了阐明 Moe 的答案,我通过以下步骤解决了一些关于 URL 查询(用于方向,使用 Volley)的类似 Google Maps API 问题:

  1. Get Android API Key (including Google Maps Directions API in my case).获取 Android API 密钥(在我的例子中包括 Google Maps Directions API)。
  2. Get "Server" API Key (which seems to be created by using a key restriction of "HTTP referrers" these days - really, it's just used to issue URL queries through HTTP).获取“服务器”API 密钥(现在似乎是通过使用“HTTP 引用”的密钥限制创建的 - 实际上,它只是用于通过 HTTP 发出 URL 查询)。
  3. Store the Android API key as a meta-data tag in the application tag in AndroidManifest.xml with android:name="com.google.android.maps.v2.API_KEY" and android:value as your key.使用android:name="com.google.android.maps.v2.API_KEY"android:value作为键,将 Android API 密钥作为meta-data标记存储在AndroidManifest.xml中的application标记中。 This is used for direct interaction with the Maps API (minus URL queries).这用于与 Maps API 的直接交互(减去 URL 查询)。
  4. Use the server API key whenever issuing URL queries.在发出 URL 查询时使用服务器 API 密钥。

I am not sure if this also applies to URL queries for the Places API, if you only need the server API key, or if there is a better solution, but this worked for me.我不确定这是否也适用于 Places API 的 URL 查询,如果您只需要服务器 API 密钥,或者是否有更好的解决方案,但这对我有用。

I imagine that it works with just the first key - the one not restricted to Android.我想它只适用于第一个键 - 不限于 Android。

Inside Google Cloud Console type Places and Activate it.在 Google Cloud Console 中输入 Places 并激活它。 Create an API Key and insert it onto your Android Studio App as you would do normally.创建一个 API 密钥并将其插入到您的 Android Studio 应用程序中,就像您通常所做的那样。 That`s it.就是这样。

I had the same problem.我有同样的问题。 For me the key was to enable billing on project.对我来说,关键是启用项目计费。 I am still using "Applications for Android" restrictions.我仍在使用“Android 应用程序”限制。 After setting up the payment method, Places Api started working.设置付款方式后,Places Api 开始工作。

Prior to using the Places SDK for Android, do the following:在使用适用于 Android 的 Places SDK 之前,请执行以下操作:

  • Follow the Get an API Key guide to get, add, and restrict an API key.按照获取 API 密钥指南获取、添加和限制 API 密钥。
  • Enable billing on each of your projects.为您的每个项目启用计费。
  • Enable the Places API for each of your projects.为您的每个项目启用 Places API。

See it there .那里看到它。

Be sure also to check out the billing plans for the Google Places API as it is not free!请务必查看 Google Places API 的计费计划,因为它不是免费的!

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

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