GMail API permissions before UserRecoverableAuthUIException

I followed the google quick launch example to configure the GMail API: https://developers.google.com/gmail/api/quickstart/android

My application successfully requests GET_ACCOUNTS permission and allows the user to select their gmail account. The selected account is stored in SharedPreferences for later access.

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

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

When I catch the exception, get the intent saved with the exception, and start the intent as a new action. I am shown a dialog giving me the ability to allow my application to send email with my GMail account:

UserRecoverableAuthIOException Intent Activity

After clicking "Allow" in this dialog box, my application sends emails without any additional problems. My application also appears on my Google Account Permissions page on the Internet, which says it has permission to send email.

Is there a way to manually run this dialog box when I first get the user account name and not expect an exception to occur?

UPDATE

I managed to extract the action and data stored in the intent, if that helps:

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

where KEY is a series of characters, possibly associated with my account or being a token.

EDIT:

Below is the code to create the Credentials object and start the account that I am using:

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

Internal constructor:

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

Where do 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); } } 
+5
source share
3 answers

Try to send mail during registration ie after the user selects an account. At this point, it will catch the exception, as soon as you get this exception, show this popup. After this user is good to go. I think there is no direct way to trigger this popup. By developers.google.com/drive/android/auth . Authorization occurs in response to an error when sending a request. Your application must be prepared to catch a UserRecoverableAuthIOException. This means that the user needs to authorize the application. Until your application is authorized by the user, your application cannot do anything.

0
source

You can try to add some permissions to the 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 to authenticate the user, but will automatically allow the intent of these permissions ...

Not tested, but may work

+1
source

To request permission to "manage drafts and sending emails" when a user selects an account, enable GmailScopes.GMAIL_COMPOSE in the scope when creating the GoogleAccountCredential .

  import com.google.api.services.gmail.GmailScopes; String[] SCOPES = { GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_LABELS }; int REQUEST_ACCOUNT_PICKER = 1001; GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff()); startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); 

For a list of scopes, see the Gmail Javadoc API .

0
source

Source: https://habr.com/ru/post/1262055/


All Articles