Search code examples
androidgoogle-drive-android-api

Stuck in Infinite Google Sign-In Loop


I've been integrating Google Drive support into my app, so when I click a button, it prompts me to sign in with my Google Account. However, I get stuck in an infinite loop whenever I try that just presents me with the same sign-in screen over and over. I'm running Android 4.4.2 (KitKat) for what it's worth.

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;
    }
}
}

The error is (repeatedly) the following: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{428a9d58: android.os.BinderProxy@428a9ce0}, message=null}

The app itself is quite large, so please let me know if any other code is required to diagnose. I've been fighting with this issue for some time now and have exhausted all Google/SO solutions for similar problems.


Solution

  • I finally figured it out!

    It turns out my partner had not authorized me using my SHA-1 key on the Google Development side, so apparently this lack of permissions manifested as a constant looping of the "Please Sign In" fragment... So of course, loading from his machine would work, but my own environment couldn't compile.

    Hope this helps anyone in the future who is stuck and thinking the problem is with their mobile device/cache!