GoogleApiClient连接失败,statusCode为SIGN_IN_REQUIRED

时间:2015-06-28 23:37:32

标签: android google-play-services

我正在尝试将我的游戏连接到 Google paly服务,但它一直告诉我连接失败,状态码 SIGN_IN_REQUIRED

logcat消息:

I / GooglePlayServicesActiv:GoogleApiClient连接失败:ConnectionResult {statusCode = SIGN_IN_REQUIRED,resolution = PendingIntent {421a8ce0:android.os.BinderProxy@421939d8}}

我已经在Setting Up Google Play Games Services

上执行了所有必要的步骤

Plz 给我任何可能导致此问题的事情。

班级代码:

package com.alnassre.ffeather.android;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;

import com.alnassre.ffeather.FFeather;
import com.alnassre.ffhelper.IGoogleServices;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;

public class AndroidLauncher extends AndroidApplication implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        IGoogleServices{


    private static final String TAG = "GooglePlayServicesActiv";

    private static final String KEY_IN_RESOLUTION = "is_in_resolution";

    /**
     * Request code for auto Google Play Services error resolution.
     */
    protected static final int REQUEST_CODE_RESOLUTION = 1;

    /**
     * Google API client.
     */
    private GoogleApiClient mGoogleApiClient;

    /**
     * Determines if the client is in a resolution state, and
     * waiting for resolution intent to return.
     */
    private boolean mIsInResolution;

    /**
     * Called when the activity is starting. Restores the activity state.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mIsInResolution = savedInstanceState.getBoolean(KEY_IN_RESOLUTION, false);
        }

        // main code
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new FFeather(this), config);
    }

    /**
     * Called when the Activity is made visible.
     * A connection to Play Services need to be initiated as
     * soon as the activity is visible. Registers {@code ConnectionCallbacks}
     * and {@code OnConnectionFailedListener} on the
     * activities itself.
     */
    @Override
    protected void onStart() {
        super.onStart();


        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Games.API)
                    .addApi(Plus.API)
                    .addScope(Games.SCOPE_GAMES)
                    .addScope(Plus.SCOPE_PLUS_LOGIN)
                            // Optionally, add additional APIs and scopes if required.
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        mGoogleApiClient.connect();
    }

    /**
     * Called when activity gets invisible. Connection to Play Services needs to
     * be disconnected as soon as an activity is invisible.
     */
    @Override
    protected void onStop() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }

    /**
     * Saves the resolution state.
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(KEY_IN_RESOLUTION, mIsInResolution);
    }

    /**
     * Handles Google Play Services resolution callbacks.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_RESOLUTION:
                retryConnecting();
                break;
        }
    }

    private void retryConnecting() {
        mIsInResolution = false;
        if (!mGoogleApiClient.isConnecting()) {
        //  mGoogleApiClient.connect();
        }
    }

    /**
     * Called when {@code mGoogleApiClient} is connected.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "GoogleApiClient connected");
        // TODO: Start making API requests.
    }

    /**
     * Called when {@code mGoogleApiClient} connection is suspended.
     */
    @Override
    public void onConnectionSuspended(int cause) {
        Log.i(TAG, "GoogleApiClient connection suspended");
        retryConnecting();
    }

    /**
     * Called when {@code mGoogleApiClient} is trying to connect but failed.
     * Handle {@code result.getResolution()} if there is a resolution
     * available.
     */

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // Show a localized error dialog.
            GooglePlayServicesUtil.getErrorDialog(
                    result.getErrorCode(), this, 0, new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            retryConnecting();
                        }
                    }).show();
            return;
        }
        // If there is an existing resolution error being displayed or a resolution
        // activity has started before, do nothing and wait for resolution
        // progress to be completed.
        if (mIsInResolution) {
            return;
        }
        mIsInResolution = true;
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
            retryConnecting();
        }
    }


    @Override
    public void signIn() {

    }

    @Override
    public void signOut() {

    }

    @Override
    public void rateGame() {

    }

    @Override
    public void submitScore(long score) {

    }

    @Override
    public void showScores() {

    }

    @Override
    public boolean isSignedIn() {
        return false;
    }
}

logcat:

06-29 01:19:41.880  26169-26192/com.alnassre.ffeather.android I/FFeather﹕ created
06-29 01:19:41.910  26169-26192/com.alnassre.ffeather.android I/FFeather﹕ character number:0
06-29 01:19:41.910  26169-26194/com.alnassre.ffeather.android V/SoundPoolThread﹕ Got message m=2, mData=1
06-29 01:19:41.910  26169-26194/com.alnassre.ffeather.android V/MediaPlayer﹕ decode(59, 28840, 16502)
06-29 01:19:41.930  26169-26194/com.alnassre.ffeather.android V/SoundPoolThread﹕ Got message m=2, mData=2
06-29 01:19:41.930  26169-26194/com.alnassre.ffeather.android V/MediaPlayer﹕ decode(60, 45392, 10400)
06-29 01:19:41.945  26169-26194/com.alnassre.ffeather.android V/SoundPoolThread﹕ Got message m=2, mData=3
06-29 01:19:41.945  26169-26194/com.alnassre.ffeather.android V/MediaPlayer﹕ decode(61, 1640, 27150)
06-29 01:19:41.960  26169-26172/com.alnassre.ffeather.android D/dalvikvm﹕ GC_CONCURRENT freed 375K, 8% free 12453K/13511K, paused 2ms+14ms, total 38ms
06-29 01:19:42.065  26169-26169/com.alnassre.ffeather.android I/GooglePlayServicesActiv﹕ GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{421a8ce0: android.os.BinderProxy@421939d8}}
06-29 01:19:42.165  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ unregisterListener::  Listener= com.badlogic.gdx.backends.android.AndroidInput$SensorListener@421403e0
06-29 01:19:42.165  26169-26169/com.alnassre.ffeather.android D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
06-29 01:19:42.165  26169-26169/com.alnassre.ffeather.android I/Sensors﹕ sendDelay --- 200000000
06-29 01:19:42.165  26169-26192/com.alnassre.ffeather.android I/AndroidGraphics﹕ paused
06-29 01:19:42.165  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ JNI - sendDelay
06-29 01:19:42.165  26169-26169/com.alnassre.ffeather.android I/SensorManager﹕ Set normal delay = true
06-29 01:19:42.175  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ unregisterListener::  Listener= com.badlogic.gdx.backends.android.AndroidInput$SensorListener@42196300
06-29 01:19:42.175  26169-26169/com.alnassre.ffeather.android D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
06-29 01:19:42.175  26169-26169/com.alnassre.ffeather.android I/Sensors﹕ sendDelay --- 200000000
06-29 01:19:42.180  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ JNI - sendDelay
06-29 01:19:42.180  26169-26169/com.alnassre.ffeather.android I/SensorManager﹕ Set normal delay = true
06-29 01:19:42.190  26169-26169/com.alnassre.ffeather.android I/AndroidInput﹕ sensor listener tear down
06-29 01:19:42.400  26169-26169/com.alnassre.ffeather.android W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
06-29 01:19:48.815  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ registerListener :: handle = 0  name= LSM330DLC Acceleration Sensor delay= 20000 Listener= com.badlogic.gdx.backends.android.AndroidInput$SensorListener@421f3558
06-29 01:19:48.825  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ registerListener :: handle = 1  name= AK8963C Magnetic field Sensor delay= 20000 Listener= com.badlogic.gdx.backends.android.AndroidInput$SensorListener@421f3fa0
06-29 01:19:48.825  26169-26169/com.alnassre.ffeather.android I/AndroidInput﹕ sensor listener setup
06-29 01:19:48.860  26169-26192/com.alnassre.ffeather.android I/AndroidGraphics﹕ resumed
06-29 01:19:49.130  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ onAccuracyChanged :: accuracy = 3
06-29 01:19:50.650  26169-26172/com.alnassre.ffeather.android D/dalvikvm﹕ GC_CONCURRENT freed 310K, 7% free 12584K/13511K, paused 12ms+2ms, total 31ms
06-29 01:19:50.650  26169-26192/com.alnassre.ffeather.android D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 15ms
06-29 01:20:18.395  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ unregisterListener::  Listener= com.badlogic.gdx.backends.android.AndroidInput$SensorListener@421f3558
06-29 01:20:18.395  26169-26169/com.alnassre.ffeather.android D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
06-29 01:20:18.395  26169-26169/com.alnassre.ffeather.android I/Sensors﹕ sendDelay --- 200000000
06-29 01:20:18.395  26169-26192/com.alnassre.ffeather.android I/AndroidGraphics﹕ paused
06-29 01:20:18.395  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ JNI - sendDelay
06-29 01:20:18.395  26169-26169/com.alnassre.ffeather.android I/SensorManager﹕ Set normal delay = true
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ unregisterListener::  Listener= com.badlogic.gdx.backends.android.AndroidInput$SensorListener@421f3fa0
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/Sensors﹕ sendDelay --- 200000000
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android D/SensorManager﹕ JNI - sendDelay
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/SensorManager﹕ Set normal delay = true
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/AndroidInput﹕ sensor listener tear down
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/AndroidGraphics﹕ Managed meshes/app: { }
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/AndroidGraphics﹕ Managed textures/app: { }
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/AndroidGraphics﹕ Managed cubemap/app: { }
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/AndroidGraphics﹕ Managed shaders/app: { }
06-29 01:20:18.400  26169-26169/com.alnassre.ffeather.android I/AndroidGraphics﹕ Managed buffers/app: { }
06-29 01:20:18.400  26169-26194/com.alnassre.ffeather.android V/SoundPoolThread﹕ Got message m=1, mData=0
06-29 01:20:18.400  26169-26194/com.alnassre.ffeather.android V/SoundPoolThread﹕ goodbye
06-29 01:20:18.400  26169-26192/com.alnassre.ffeather.android V/SoundPoolThread﹕ return from quit
06-29 01:20:18.405  26169-26192/com.alnassre.ffeather.android V/SoundPoolThread﹕ return from quit
06-29 01:20:18.405  26169-26192/com.alnassre.ffeather.android I/AndroidGraphics﹕ destroyed
06-29 01:20:18.680  26169-26169/com.alnassre.ffeather.android W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
06-29 01:20:18.860  26169-26169/com.alnassre.ffeather.android W/SurfaceView﹕ CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false

3 个答案:

答案 0 :(得分:3)

在Android工作室中,像我这样的初学者经常会忽略一个额外且非常重要的步骤。

当您通过Android studio运行时,您需要手动指定用于构建和签署APK的密钥库。这应该是您在Google Developer Console中为生成API密钥而输入的SHA1证书签名的密钥库。

以下是您的操作方法:

  1. 如果您没有现有的密钥库或找不到密钥库,可以转到构建 - >创建密钥库。 生成签名APK - > 创建新 Keystore Android Studio

  2. 创建密钥库后,需要将其添加到项目设置中,以便用于对APK进行签名。转到文件 - &gt; 项目结构 - &gt; 选择您的模块(例如:app) - &gt; <强>登录 Keystore Android Studio

  3. 然后,您需要将在步骤2中创建的签名配置指定为构建类型 Keystore Android Studio

  4. 希望这有助于某人!

答案 1 :(得分:2)

在任何文件上都没有看到这个解决方案,对于像我这样的初学者需要提及,

添加缺少的权限:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

成为:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alnassre.ffeather.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="22" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="com.alnassre.ffeather.android.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <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.gms.games.APP_ID" android:value="@string/app_id" />
        <meta-data android:name="com.google.android.gms.appstate.APP_ID" android:value="@string/app_id" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
    </application>

</manifest>

答案 2 :(得分:0)

一个简单的解决方案是简单地使用Android的调试密钥存储区OAuth client ID创建一个测试SHA-1

转到API Console->凭据->创建凭据-> OAuth客户端ID-> Android

获取调试SHA-1

C:\Program Files\Android\Android Studio\jre\bin

keytool -exportcert -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore

密码

android

输入SHA-1->输入应用程序包名称->创建

默认情况下,Android Studio使用此密钥库对调试APK进行签名,因此它应立即运行。