LogIn to google drive on android without adding the google account to the device

user2924714 picture user2924714 · Oct 18, 2015 · Viewed 7k times · Source

I develop an android application that needs to allow read/write access to user's google drive. In addition, it is supposed to run on public devices (meaning that many people might use same device to access their google drive account). In such circumstances, it is unacceptable to add each user's account to Android Account Manager. Unfortunately, all official guides, use the authentication with the google account registered on device. The popup of selecting existing google accounts from device or adding a new one appears.

protected void onStart() {
        super.onStart();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                            // Optionally, add additional APIs and scopes if required.
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        mGoogleApiClient.connect();
    }



    /**
     * Called when {@code mGoogleApiClient} is trying to connect but failed.
     * Handle {@code result.getResolution()} if there is a resolution
     * available.
     */
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // Show a localized error dialog.
            GooglePlayServicesUtil.getErrorDialog(
                    result.getErrorCode(), this, 0, new OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            retryConnecting();
                        }
                    }).show();
            return;
        }
        // If there is an existing resolution error being displayed or a resolution
        // activity has started before, do nothing and wait for resolution
        // progress to be completed.
        if (mIsInResolution) {
            return;
        }
        mIsInResolution = true;
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
            retryConnecting();
        }
    }

What is the workaround? I need to login to google account (actually only to the drive) without saving the account on the device. I just need to login and logout when finished, without leaving any personal info on the device. How can I do it?

Answer

seanpj picture seanpj · Oct 20, 2015

It is not possible. Only accounts registered on the device (authenticated by users themself) can be selected/used to connect to GooDrive. Even if you can use construct like:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                            // Optionally, add additional APIs and scopes if required.
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .setAccountName([[email protected]])
                    .build();

(please notice the .setAccountName([[email protected]])), the email still HAS TO BE one of the device registered accounts. The app can't address any accounts the user her/himself did not authenticate, it would be security no-no, right?

Good Luck