1

[EDIT] - For Bounty

I am trying to connect my android application with Google Drive to sync, MP3 audio files. Need working code/examples (apart from the links given below) for connecting with Google Drive using the GoogleDriveAndroidApi and GoogleApiClient.


This question has been asked many number of times, but none of them had a solution for the question. So, i am asking this again.

I am working on Google Drive Android API for the first time, and couldn't pass through the "Choose account for 'APP NAME'" screen. Here is what i doing as of now:

  1. Tried to follow the urls below:

https://developers.google.com/drive/android/get-started

https://github.com/googledrive/android-quickstart

https://www.numetriclabz.com/integrate-google-drive-in-android-tutorial/

  1. Using compile 'com.google.android.gms:play-services-drive:10.0.1' in my build.gradle (if that makes any difference)

  2. Code tried so far:

     public class ARCBackupActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
                GoogleApiClient.OnConnectionFailedListener {
    
            private static final int REQUEST_CODE = 1;
    
            private GoogleApiClient mGoogleApiClient;
            private Toolbar mToolbar;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.arc_activity_backup);
    
                mToolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(mToolbar);
    
                getSupportActionBar().setDisplayShowTitleEnabled(false);
                getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            }
    
    
            @Override
            protected void onResume() {
                super.onResume();
                if (mGoogleApiClient == null) {
                    // Create the API client and bind it to an instance variable.
                    // We use this instance as the callback for connection and connection
                    // failures.
                    // Since no account name is passed, the user is prompted to choose.
                    mGoogleApiClient = new GoogleApiClient.Builder(this)
                            .addApi(Drive.API)
                            .addScope(Drive.SCOPE_FILE)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .build();
                }
                // Connect the client. 
                mGoogleApiClient.connect();
            }
    
            @Override
            protected void onPause() {
                if (mGoogleApiClient != null) {
                    mGoogleApiClient.disconnect();
                }
                super.onPause();
            }
    
            @Override
            protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
                switch (requestCode) {
    
                    case REQUEST_CODE:
    
                        if (resultCode == Activity.RESULT_OK) {
                            mGoogleApiClient.connect();
                        }
                        break;
                }
            }
    
            @Override
            public void onConnectionFailed(ConnectionResult result) {
                // Called whenever the API client fails to connect.
                Log.i(Const.DEBUG, "GoogleApiClient connection failed: " + result.toString());
                if (!result.hasResolution()) {
                    // show the localized error dialog.
                    GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
                    return;
                }
                // The failure has a resolution. Resolve it.
                // Called typically when the app is not yet authorized, and an
                // authorization
                // dialog is displayed to the user.
                try {
                    result.startResolutionForResult(this, REQUEST_CODE);
                } catch (IntentSender.SendIntentException e) {
                    Log.e(Const.DEBUG, "Exception while starting resolution activity", e);
                }
            }
    
            @Override
            public void onConnected(Bundle connectionHint) {
                Log.d(Const.DEBUG, "API client connected.");
    
                //saveFileToDrive();
            }
    
            @Override
            public void onConnectionSuspended(int cause) {
                Log.d(Const.DEBUG, "GoogleApiClient connection suspended");
            }
        }
    

    What is that i am missing here? Any complete example with good explanation is much appreciated.

    Let me know if i need to give more info to solve the issue

    [EDIT 1]

    If i add Plus.API while building the googleApiClient, it solves the problem of getting stuck in the loop of choosing the account. But, Plus.API is deprecated, is there a workaround for this just by using GoogleApiClient?

    Here is the changed code:

     @Override
        protected void onResume() {
            super.onResume();
    
            if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
            }
    
    
            if (mGoogleApiClient == null) {
                // Create the API client and bind it to an instance variable.
                // We use this instance as the callback for connection and connection
                // failures.
                // Since no account name is passed, the user is prompted to choose.
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(Drive.API)
                        .addApi(Plus.API)
                        .addScope(Drive.SCOPE_FILE)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .build();
            }
            // Connect the client.
            mGoogleApiClient.connect();
        }
    

Still looking for the correct way of implementing...

avinash
  • 1,744
  • 24
  • 40
Vamsi Challa
  • 11,038
  • 31
  • 99
  • 149
  • "Since no account name is passed, the user is prompted to choose" - does that happen? If you get that screen, then it works. Your error is that you need to login to a valid account – OneCricketeer Dec 30 '16 at 15:30
  • @cricket_007, yes i get the choose account screen. Once i select the account, the same screen is shown again and again as a loop. – Vamsi Challa Dec 30 '16 at 15:34
  • @cricket_007, can you check my edited code and let me know what is the best way to implement GoogleApiClient correctly? – Vamsi Challa Dec 30 '16 at 15:59
  • 1
    I've never done it myself, but if the plus api is deprecated, I'd read the documentation about what the alternative is – OneCricketeer Dec 30 '16 at 18:23
  • Try this :go to the google api console from your gmail account. Here you will see a disable button in front of Google drive api. just right to that you will see a gear or setting button click on it and generate oAuth token. – PN10 Jan 03 '17 at 11:54
  • @VamsiChalla do this thing let me if your issue sort out or not?? or is their any other issue comes up?? – PN10 Jan 03 '17 at 11:57
  • @VamsiChalla : In onactivityResult if (resultCode == Activity.RESULT_OK) { Then do you can check if you are getting list of files} – avinash Jan 04 '17 at 08:54
  • @VamsiChalla : If (resultCode == Activity.RESULT_OK) { mGoogleApiClient.connect(); } : this might be issue. Once u got the result ok, the u can proceed with your stuff. – avinash Jan 06 '17 at 05:08
  • You can refer to this answer, if you have not specified the Signing Keystore in Android studio: http://stackoverflow.com/a/42441460/673910 – Anurag Feb 25 '17 at 12:24

2 Answers2

3

It looks like that the problem you have is because of

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

Let me explain. When the account chooser appears it makes your activity to call onPause(). And in onPause() you calls mGoogleApiClient.disconnect().

As result, you have new GoogleApiClient after account selection (when onResume and onActivityResult is called).

To fix the problem just place mGoogleApiClient.connect() and mGoogleApiClient.disconnect() to onStart()/onStop() methods of your activity or use enableAutoManage(Activity, OnConnectionFailedListener) for you GoogleApiClient.

Happy coding! :)

Alexander Bilchuk
  • 1,790
  • 1
  • 12
  • 17
1

Well I was going through issues for Google drive on github ,i found the similar issue their go through @iuribtt answer

Here is link

Google Drive Issues

In case this link got expired I am posting his answer :

Try to use "keytool -exportcert -alias androiddebugkey -keystore C:\Users\XXXXX.android\debug.keystore -list -v" and not the keystore that you generate, once you want the debug mode.

The path for the "debug.keystore" and the password is "android" https://support.google.com/cloud/answer/6158849?hl=en#android

And after that create the project in https://developers.google.com/mobile/add

Finally, enable the Drive API in the https://console.developers.google.com

Hope this Helps!!!Cheers!!!

PN10
  • 1,888
  • 3
  • 23
  • 34