简体   繁体   English

Google Awareness API,无效的API密钥,崩溃

[英]Google Awareness API, Invalid API key, Crash

This is my first attempt at creating an android application. 这是我第一次尝试创建一个Android应用程序。 In trying to use the Awareness API provided by Google I receive a SecurityException in logcat, and the error: 在尝试使用Google提供的Awareness API时,我在logcat中收到SecurityException,错误如下:

"Invalid API Key for package = com.example.android.project. Status code received = 12". “package = com.example.android.project的API密钥无效。收到的状态代码= 12”。

Meaning, mGoogleApiClient.connect() causes the app to crash every time. 意思是,mGoogleApiClient.connect()会导致应用每次都崩溃。 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, ie not locked to any project and it still will not work. 这个错误没有意义,因为在Developer Console中我设置API密钥没有任何限制,希望它能解决这个问题,即没有锁定到任何项目,它仍然无法正常工作。 And the API is enabled in the console. 并且在控制台中启用了API。

I've tried suggestions from other threads: Unable to connect to Google API CLient? 我尝试过其他主题的建议: 无法连接到Google API CLient? , Receiving error when trying to connect with Google Awareness services , etc. But the problem remains. 尝试连接Google Awareness服务时收到错误等。但问题仍然存在。

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: 最后我的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 我正在三星Galaxy S6,Android版本:7.0上进行测试

Print this to the console: (new Status(403)).getStatusMessage() . 将其打印到控制台:( (new Status(403)).getStatusMessage() It should show status message of the error. 它应该显示错误的状态消息。 Read more here 在这里阅读更多

It seems like your API_KEY is set up incorrectly in the AndroidManifest: note that the right package name is 您的API_KEY似乎在AndroidManifest中设置不正确:请注意正确的包名称是

"com.google.android.awareness.API_KEY" “com.google.android.awareness.API_KEY”

and not 并不是

"com.google.awareness.API_KEY" “com.google.awareness.API_KEY”

as you seem to have it. 因为你似乎拥有它。 See https://developers.google.com/awareness/android-api/get-started 请参阅https://developers.google.com/awareness/android-api/get-started

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. 如果有人面临着正确舱单申报类似的问题,因为在谷歌中提到这里可以尝试使用生成密钥库调试测试目的,并发布密钥库替换相同,而要生产的谷歌开发者控制台的API密钥。

How generate API key? 如何生成API密钥? Please check out Google's word itself Here 在这里查看谷歌的单词

Hope it will help someone in future. 希望它将来有助于某人。

In my case issue is resolved by adding following meta data tag in manifests.xml file 在我的情况下,通过在manifests.xml文件中添加以下元数据标记来解决问题

<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" />

如果您在Google Play控制台的预启动报告中收到此错误,请在预启动设置中选中“选择停用生成启动前报告,其中X已重新签名”。

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

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