Android 陷入无限谷歌登录循环

Android 陷入无限谷歌登录循环,android,google-drive-android-api,Android,Google Drive Android Api,我一直在将Google Drive支持集成到我的应用程序中,所以当我单击一个按钮时,它会提示我使用我的Google帐户登录。然而,每当我尝试的时候,我就会陷入一个无限循环中,那只是一遍又一遍地在屏幕上显示同一个登录。我运行Android 4.4.2(KitKat)是为了它的价值 import android.app.Activity; import android.content.Intent; import android.content.IntentSender; import androi

我一直在将Google Drive支持集成到我的应用程序中,所以当我单击一个按钮时,它会提示我使用我的Google帐户登录。然而,每当我尝试的时候,我就会陷入一个无限循环中,那只是一遍又一遍地在屏幕上显示同一个登录。我运行Android 4.4.2(KitKat)是为了它的价值

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.OpenFileActivityBuilder;
import com.neilcpower.surveildroid.R;

import butterknife.ButterKnife;
import timber.log.Timber;

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

public static final String FILE_ID = "File ID";

private static final int REQUEST_CODE_OPENER = 48957;
private static final int REQUEST_CODE_RESOLUTION = 1;

private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file_picker);
    ButterKnife.bind(this);
}

@Override
protected void onResume() {
    super.onResume();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

@Override
protected void onPause() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onPause();
}

@Override
public void onConnected(Bundle connectionHint) {
    Timber.i("Connected To Google API Client");
    try {
        IntentSender intentSender = Drive.DriveApi
                .newOpenFileActivityBuilder()
                .setMimeType(new String[]{"text/plain", "text/html"})
                .build(mGoogleApiClient);
        try {
            startIntentSenderForResult(
                    intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            Timber.e(e, "Unable to send intent");
        }
    } catch (IllegalStateException e) {
        // TODO Why is the first time onConnected called the client is not connected?
        mGoogleApiClient.connect();
    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Timber.i("GoogleApiClient connection failed: %s", connectionResult.toString());
    if (!connectionResult.hasResolution()) {
        GoogleApiAvailability.getInstance().getErrorDialog(this, connectionResult.getErrorCode(), 0).show();
        return;
    }
    try {
        connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (IntentSender.SendIntentException e) {
        Timber.e(e, "Exception while starting resolution activity");
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_OPENER:
            if (resultCode == RESULT_OK) {
                DriveId driveId = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
                Intent returnIntent = new Intent();
                returnIntent.putExtra(FILE_ID, driveId.encodeToString());
                setResult(Activity.RESULT_OK, returnIntent);
            }
            finish();
            break;
        case REQUEST_CODE_RESOLUTION:
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            }
            break;
    }
}
}
错误(重复)如下:ConnectionResult{statusCode=SIGN_IN_REQUIRED,resolution=PendingEvent{428a9d58:android.os。BinderProxy@428a9ce0},message=null}


该应用程序本身相当大,因此请让我知道是否需要任何其他代码进行诊断。我已经为这个问题争论了一段时间,并且已经为类似的问题用尽了所有Google/SO解决方案。

您正在每个
onResume
中创建新的Google API客户端。当您未登录时,客户端会要求用户登录。这将在活动中调用
onPause
,从而断开与客户端的连接,从而永远不会检索登录结果。用户登录后,您将返回活动,并调用
onResume
。这将创建一个未登录的新GoogleAPI客户端,因此它会要求用户再次登录

onCreate
中创建客户端


另外,
connect
应该转到
onStart
disconnect
onStop
,我终于明白了

事实证明,我的合作伙伴没有授权我在谷歌开发端使用我的SHA-1密钥,所以很明显,这种权限的缺乏表现为“请登录”片段的不断循环。。。所以,当然,从他的机器加载是可行的,但我自己的环境无法编译


希望这能帮助将来陷入困境并认为问题出在移动设备/缓存上的任何人

我试过了,但没用。这也与谷歌的例子相反:我正在与一名团队成员一起进行一个项目,当从他的计算机编译时,两个分支中的相同提交将避免循环,但当我从我的计算机编译时,则不会。有什么想法吗?“这是非常令人沮丧的。”罗马大学的领导说,这个例子适用于不同的阶层。对于GoogleapClient,这是我发布的方式:对于将来遇到此问题的任何人:确保您同时授权debug.keystore和production.keystore,以避免您的许多麻烦:)