I'm trying to login user on Google and try to upload/download file from Google Drive, this is my activity to login
import android.content.Intent;
import android.content.IntentSender;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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;
public class google extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case 3:
if (resultCode == RESULT_OK) {
mGoogleApiClient.connect();
}
break;
}
}
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i("fail", "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
try {
result.startResolutionForResult(this, 3);
} catch (IntentSender.SendIntentException e) {
Log.e("ing", "Exception while starting resolution activity", e);
}
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i("conn", "API client connected.");
}
@Override
public void onConnectionSuspended(int cause) {
Log.i("susp", "GoogleApiClient connection suspended");
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
}
the only think i get is this prompt to choose account again and again. this is the error printed in logcat:
GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{aa798dc: android.os.BinderProxy@17c2e544}, message=null}
i'm sure i'm doing something wrong on activity result or onconnectionfailed, but i can't figure out where and why..
Have you tried following the Google Document about Connecting and Authorizing the Google Drive Android API?
Authorization for the Google Drive Android API is handled by the
GoogleApiClient
. This is typically created in an activity'sonCreate()
method.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstance);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
After you create the client, you must connect it for authorization to occur.
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
Try using this onCreate code and instead of onResume implement it as onStart without include the scopes again just calling the mGoogleApiClient.connect();
.
The document also provide Error Handlers which can help you verify if your implementation is correct.
Note: The Google Drive Android API currently only supports drive.file and drive.appfolder authorization scopes. If your application requires additional permissions or features not yet available in the Drive Android API, you must use the Google APIs Java Client.
I hope this helps you solve your problem.