Android: Google Maps Android API: Authorization fa

2020-02-12 21:42发布

When my app tries to use the Google Maps API, I get the following logcat error as soon as I try to access Google Map API data (scroll right to see the good stuff!):

02-02 15:39:35.329 23868-24511/com.somepackage.name E/Google Maps Android API: Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
02-02 15:39:35.339 23868-24511/com.somepackage.name E/Google Maps Android API: Ensure that the following correspond to what is in the API Console:
                                                                                            API Key: AI~~~~~~~~~~~~~~~~~~~~~~~~~~ZwmI​
                                                                                            Package Name: com.somepackage.name
                                                                                            Certificate Fingerprint: 50:~~~~~~~~~~~~~~~~~~~~:D5

Here is an image of the Google Developers Support Console with the Fingerprint and the API Key (and yes, they DO match!). I have also waited for the Google servers to update (5 minutes according to the dox; I have waited hours).

enter image description here

And here is my project's primary AndroidManifest.xml (edited for brevity):

<?xml version="1.0" encoding="utf-8"?>
...
<!--
 The following two permissions are not required to use
 Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    ...
    <!--The key to use Google's map API -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIz~~~~~~~~~~~~~~~~~~~~~~~~~~~~wmI​"/>

    <activity
    ...
    </activity>
    ...
</application>

I have a Premium Support account with Google, but have been unable to contact them so far. Does anyone have any idea why I'm getting this Authorization Failure?

1条回答
趁早两清
2楼-- · 2020-02-12 22:08

After many hours, I figured out the answer plus one very important gotcha!

Situation

I need to re-explain as details I didn't know where important are actually paramount.

I inherited this project, so its history was unknown. Originally the program used the free version of Google Maps, which worked fine. Since this is a large company, they went ahead and paid for the full premium subscription service. Programmers added the new libraries and made the few code modifications to use those libraries.

But they still used the original (free) authorization key. This caused Google's servers to continue to treat the data as if it were a free version.

No one noticed this because the free Goole Maps API only has issues after some number (2500?) of API calls in one day. Our traffic only recently became high enough for this to be an issue. (After the limit, Google API sends an error message instead of data.)

Consequently any changes I made seemed to have no effect. And changing the keys in the code caused the authentication errors (see in question).

Solution

I had to make a new authentication key using the new premium libraries and console. This changed the Google server which of course caused all versions of the app already in existence to cease functioning!

The new authentication key, placed in the manifest, gets recognized by the Google API and everything works, yay! But we still need to release a new version in the Google Store.

Gotcha #1

The AndroidManifest.xml command that is listed in the docs DID NOT WORK:

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AI............................mI​"/>

This is the correct name that actually works:

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AI............................mI​" />

Gotcha #2

But when testing, I still got all sorts of errors. Why? because android devices maintain caches of the authentication keys. Clearing the data (or better, uninstalling) cleaned out those caches. Now everything works, yay.

Moral: when dealing with API keys, authentication, certificates, and anything similar, uninstall EVERY TIME! :)

查看更多
登录 后发表回答