Google Awareness API, Invalid API key, Crash

2019-07-02 15:14发布

This is my first attempt at creating an android application. In trying to use the Awareness API provided by Google I receive a SecurityException in logcat, and the error:

"Invalid API Key for package = com.example.android.project. Status code received = 12".

Meaning, mGoogleApiClient.connect() causes the app to crash every time. Is there anyway to know what the status code means?

[BaseServerTask]Server task (PingTask) got error statusCode=403.
    com.android.volley.AuthFailureError
    at com.android.volley.toolbox.BasicNetwork.performRequest(:com.google.android.gms:32)
    at lbf.performRequest(:com.google.android.gms:3)
    at com.android.volley.NetworkDispatcher.run(:com.google.android.gms:11)
    07-01 15:54:24.303 4839-19074/? E/ctxmgr: [ContextManager3PCredentialsVerifier]Failed ping response: network status=12
    07-01 15:54:24.304 4839-19081/? E/AbstractServiceBroker: Getting service failed

java.lang.SecurityException: Invalid API Key for package = com.example.android.project. Status code received = 12
    at cpm.a(:com.google.android.gms:19)
    at cpp.a(:com.google.android.gms:46)
    at ktv.a(:com.google.android.gms:41)
    at kwh.onTransact(:com.google.android.gms:8)
    at android.os.Binder.transact(Binder.java:507)
    at bxh.onTransact(:com.google.android.gms:2)
    at android.os.Binder.execTransact(Binder.java:573)
    07-01 15:54:24.520 19508-19508/com.example.android.project E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.project, PID: 19508

java.lang.SecurityException: Invalid API Key for package = com.example.android.project. Status code received = 12
    at android.os.Parcel.readException(Parcel.java:1693)
    at android.os.Parcel.readException(Parcel.java:1646)
    at com.google.android.gms.common.internal.zzax.zza(Unknown Source)
    at com.google.android.gms.common.internal.zzd.zza(Unknown Source)
    at com.google.android.gms.internal.zzbcj.zzpV(Unknown Source)
    at com.google.android.gms.internal.zzbcn.run(Unknown Source)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at com.google.android.gms.internal.zzbgx.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:762)

This error does not make sense, because in the Developer Console I set the API key to have no restriction hoping it would solve this, i.e. not locked to any project and it still will not work. And the API is enabled in the console.

I've tried suggestions from other threads: Unable to connect to Google API CLient?, Receiving error when trying to connect with Google Awareness services, etc. But the problem remains.

public class MainActivity extends AppCompatActivity implements 
GoogleApiClient.OnConnectionFailedListener, 
GoogleApiClient.ConnectionCallbacks {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Connect to Awareness API
        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Awareness.API)
            .build();
        mGoogleApiClient.connect(); // the app crashes here

    }
}

Lastly my AndroidManifest:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.android.project">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/appicon"
    android:label="@string/app_name"
    android:roundIcon="@drawable/appicon"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.awareness.API_KEY"
        android:value="MY_API_KEY"/>
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="MY_API_KEY"/>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

</manifest>

I don't know what to do, does anyone have any suggestions?

I'm testing this on an Samsung Galaxy S6, Android Version: 7.0

5条回答
\"骚年 ilove
2楼-- · 2019-07-02 15:26

If someone facing similar issue with correct manifest declaration as mentioned in Google here can try generating API key on Google Developer Console using debug keystore for testing purpose and replacing same with release keystore while going to production.

How generate API key? Please check out Google's word itself Here

Hope it will help someone in future.

查看更多
Lonely孤独者°
3楼-- · 2019-07-02 15:29

Print this to the console: (new Status(403)).getStatusMessage(). It should show status message of the error. Read more here

查看更多
Luminary・发光体
4楼-- · 2019-07-02 15:34

It seems like your API_KEY is set up incorrectly in the AndroidManifest: note that the right package name is

"com.google.android.awareness.API_KEY"

and not

"com.google.awareness.API_KEY"

as you seem to have it. See https://developers.google.com/awareness/android-api/get-started

查看更多
Root(大扎)
5楼-- · 2019-07-02 15:37

If you get this error in the Pre-launch reports in the Google Play Console, check "Opt-out of generating pre-launch reports where X is re-signed" in the Pre-launch settings.

查看更多
何必那么认真
6楼-- · 2019-07-02 15:41

In my case issue is resolved by adding following meta data tag in manifests.xml file

<meta-data
            android:name="com.google.android.awareness.API_KEY"
            android:value="API_KEY"/>

And there must exist the following tag as well

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="API_KEY" />
查看更多
登录 后发表回答