Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMail API Grant Permissions Before UserRecoverableAuthUIException

I've followed the quickstart example from Google to setup the GMail API: https://developers.google.com/gmail/api/quickstart/android

My app successfully requests the GET_ACCOUNTS permission and allows the user to select his/her gmail account. The selected account is saved in SharedPreferences for later access.

Through an IntentService, my app sends an email. I have followed the instructions located here: https://developers.google.com/gmail/api/guides/sending and included the activation.jar, additional.jar, and mail.jar libraries as needed. When I send the email with the below code, I get a UserRecoverableAuthUIException:

message = service.users().messages().send(userId, message).execute();

When I catch the exception, get the intent stored with the exception, and start the intent as a new activity, I am shown a dialog giving me the chance to allow my app to send email with my GMail account:

UserRecoverableAuthIOException Intent Activity

After pressing 'Allow' on this dialog, my app sends emails without any further issues. My app also appears on my Google account's permissions page online saying it has permission to send email.

Is there a way to manually trigger this dialog when I first acquire the user's account name rather than waiting for the exception to occur?

UPDATE

I have been able to extract the action and data stored in the intent if that helps any:

  • action: com.google.android.gms.ui.UNPACKING_REDIRECT
  • data: intent://com.google.android.gms.auth.uiflows.common/KEY

where KEY is a series of characters, probably linked to my account or are a token.

EDIT:

The following is the code to create the Credentials object and starting the account picker activity which I'm using:

private GoogleAccountCredential mCredential;
private static final String[] SCOPES = { GmailScopes.GMAIL_COMPOSE };

Inside constructor:

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

Where I get the account:

private void chooseGMailAccount() {
        String accountName = this.getSharedPreferences(getString(R.string.shared_pref_main),
                Context.MODE_PRIVATE)
                .getString(getString(R.string.srd_pref_gmail_account), null);
        if (accountName != null) {
            mCredential.setSelectedAccountName(accountName);
            configureGMailAPI();
        } else {
            startActivityForResult(
                    mCredential.newChooseAccountIntent(),
                    REQUEST_ACCOUNT_PICKER);
        }
    }
like image 261
Programmer001 Avatar asked Dec 29 '16 22:12

Programmer001


1 Answers

You could try to add a few permissions to your manifest

<uses-permission android:name="com.google.android.gm.permission.WRITE_GMAIL" />
<uses-permission android:name="com.google.android.gm.permission.AUTO_SEND" />

And it should still need oAuth for user authentication but will allow the intent automatically these permissions ...

Haven't tested it but it might work

like image 50
pottedmeat7 Avatar answered Sep 24 '22 04:09

pottedmeat7