陷入无限谷歌登录循环

时间:2016-03-15 21:56:21

标签: android google-drive-android-api

我一直在将Google云端硬盘支持整合到我的应用中,因此当我点击一个按钮时,它会提示我使用我的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 = PendingIntent {428a9d58:android.os.BinderProxy@428a9ce0},message = null}

该应用程序本身非常大,所以如果诊断需要任何其他代码,请告诉我。我已经和这个问题争论了一段时间,并且已经用尽所有Google / SO解决方案来解决类似问题。

2 个答案:

答案 0 :(得分:2)

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

onCreate

中创建客户端

此外,connect应转到onStartdisconnect转到onStop

答案 1 :(得分:1)

我终于明白了!

事实证明我的合作伙伴没有授权我在Google开发方面使用我的SHA-1密钥,因此显然这种缺乏权限表现为不断循环播放"请登录"片段...当然,从他的机器上加载会起作用,但我自己的环境无法编译。

希望这可以帮助将来陷入困境并且想到问题的人是他们的移动设备/缓存!