0

Just a quick snippet of my code:

Initialize:

    mCredential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), SCOPES)
            .setBackOff(new ExponentialBackOff());

Then I start the chooser:

startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);

Then in the ActivityForResult

if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
                    String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                    if (accountName != null) {
                        PersistentUtil.persistGmailName(this, accountName);
                        mCredential.setSelectedAccountName(accountName);
                        checkDataAccess();
                    }
                }

Where checkDataAccess:

java.lang.Thread thread = new java.lang.Thread(new Runnable() {
        @Override
        public void run() {
            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            Gmail mService = new com.google.api.services.gmail.Gmail.Builder(
                    transport, jsonFactory, mCredential)
                    .setApplicationName(getResources().getString(R.string.app_name))
                    .build();

            try {
                Gmail.Users users = mService.users();
                Gmail.Users.Messages messages = users.messages();
                Gmail.Users.Messages.List me = messages.list("me");
                ListMessagesResponse temp = me.execute();
                // if no error is thrown then, we are good to go
                onGmailFinished();
                //                    parseMessageList(mService, temp);
            } catch (UserRecoverableAuthIOException e) {
                startActivityForResult(
                        ((UserRecoverableAuthIOException) e).getIntent(),
                        REQUEST_AUTHORIZATION);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();

ISSUE

ListMessagesResponse temp = me.execute(); will eventually call a getAccounts() method which searches for the user in the accounts that are configured on the device. It'll return null, if the GET_ACCOUNTS permission is not granted.

HOW can I make it so that I don't depend on the GET_ACCOUNTS permission, some way of configuring a gmail account independently of the device's accounts? I don't want to request the GET_ACCOUNTS permission.

Is there a way of doing this or do I always have to go via the intentChooser and depend on the GET_ACCOUNTS permission afterwards ? :(

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106

1 Answers1

0

It says from this SO post that it is better to use an Account Picker wherein it returns an intent to an Activity that prompts the user to choose from a list of accounts.

If your app has the GET_ACCOUNTS permission and there's only one account, you get it right away. If your app doesn't have it, or if there are more than one account, users get a prompt so they can authorize or not the action.

Your app needs to include the Google Play Services but it doesn't need any permissions.

Basic code sample:

private static final int REQUEST_CODE_EMAIL = 1;
private TextView email = (TextView) findViewById(R.id.email);

// ...

try {
    Intent intent = AccountPicker.newChooseAccountIntent(null, null,
            new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
    startActivityForResult(intent, REQUEST_CODE_EMAIL);
} catch (ActivityNotFoundException e) {
    // TODO
}

// ...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
        String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        email.setText(accountName);
    }
}
Jessica Rodriguez
  • 2,899
  • 1
  • 12
  • 27