Receiving error when trying to connect with Google

2019-02-19 13:53发布

I'm trying to work with google's awareness api, and register instance of GoogleApiClient to awareness api, but i'm receiving an error which i can't find anywhere.

I've followed the manual, and enabled awareness services in the api consule. https://developers.google.com/awareness/android-api/get-started

This is how my manifest looks like :

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.awarenesstests">

    <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="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

</manifest>

Very straightforward registration :

 GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Awareness.API).build();
 googleApiClient.connect();

This is the error i'm receiving - Invalid API Key for package = com.test.awarenesstests .Status code received = 6

Notice that there's something weird in the start of the log, a ping is made to google services and returning 403, it looks related to the final error.

07-10 15:35:50.745 29260-29490/? E/Volley: [6925] BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/usercontext/v1/controllerhub/ping
07-10 15:35:50.750 29260-29260/? E/ctxmgr: [BaseServerTask]Server task (PingTask) got error response.
                                           com.android.volley.AuthFailureError
                                               at com.android.volley.toolbox.BasicNetwork.performRequest(:com.google.android.gms:159)
                                               at ipb.performRequest(:com.google.android.gms:64)
                                               at com.android.volley.NetworkDispatcher.run(:com.google.android.gms:113)
07-10 15:35:50.755 29260-15589/? W/ctxmgr: [ContextManager3PCredentialsVerifier]Received a failed ping response with status code = 6
07-10 15:35:50.755 29260-29275/? E/AbstractServiceBroker: Getting service failed
                                                          java.lang.SecurityException: Invalid API Key for package = com.test.awarenesstests .Status code received = 6
                                                              at bti.a(:com.google.android.gms:109)
                                                              at btl.a(:com.google.android.gms:5046)
                                                              at iht.a(:com.google.android.gms:592)
                                                              at ikn.onTransact(:com.google.android.gms:824)
                                                              at android.os.Binder.execTransact(Binder.java:446)
07-10 15:35:50.756 9603-9603/com.test.awarenesstests D/AndroidRuntime: Shutting down VM
07-10 15:35:50.757 9603-9603/com.test.awarenesstests E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.test.awarenesstests, PID: 9603
                                                                       java.lang.SecurityException: Invalid API Key for package = com.test.awarenesstests .Status code received = 6
                                                                           at android.os.Parcel.readException(Parcel.java:1546)
                                                                           at android.os.Parcel.readException(Parcel.java:1499)
                                                                           at com.google.android.gms.common.internal.zzu$zza$zza.zza(Unknown Source)
                                                                           at com.google.android.gms.common.internal.zzd.zza(Unknown Source)
                                                                           at com.google.android.gms.internal.zzqb$zzc.zzapi(Unknown Source)
                                                                           at com.google.android.gms.internal.zzqb$zzf.run(Unknown Source)
                                                                           at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                           at com.google.android.gms.internal.zzrs.run(Unknown Source)
                                                                           at java.lang.Thread.run(Thread.java:818)

5条回答
Rolldiameter
2楼-- · 2019-02-19 13:56

Slightly different scenario in my case. I had 3 build variants

  • Development
  • Staging
  • Production

So I had 3 separate google-services.json each for 3 variants. Along with this i had put the below code in manifest with developement API key.

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

When i made signed apk with Production enviroment, i was getting the same error. App was taking API key from AndroidManifest.xml instead of google-services.json.

So i commented out the API key in the manifest file, then problem got resolved.

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-19 13:58

The only thing you can read out of the stacktrace is that your API-Key isn't accepted. So you get an 403-forbidden. Do you generated the api-key right? Maybe your SHA1 fingerprint isn't the right?

查看更多
做自己的国王
4楼-- · 2019-02-19 14:00

Enable the Awareness Api from the developers console and it will work fine

查看更多
爷、活的狠高调
5楼-- · 2019-02-19 14:14

I had the same issue. Check the applicationId in your application's build.gradle file. For me the applicationId and the package name i setted on the console.developers.google.com at my API key were not the same. I guess they have to be the same. After fixing the names it worked for me. I hope it will work for you.

查看更多
Viruses.
6楼-- · 2019-02-19 14:15

This is an known bug. When GoogleApiClient encounters a network error in connect(), it throws a SecurityException.

Extremely annoying but you are not doing anything wrong (as long as you are using the right key)

查看更多
登录 后发表回答